Help me find a better way to do the thing!

agolangf · · 237 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have enjoyed tinkering with Go so I decided to try to write something non-trivial: bindings for the <a href="" rel="nofollow">Remember The Milk API</a>. Once I get this done, I have an idea for an app which will manage related tasks, but I&#39;m not there yet.</p> <p>I made great progress in a reasonable amount of time, and am able to log into the server and send echo pings. I started to notice some annoying repetition, though, and was hoping that you folks could help me simplify my code.</p> <p>I have attached a few snippets of code below for examples. The whole thing (as far as I&#39;ve gotten) is on the first-pass branch at <a href="" rel="nofollow"></a> if you would like to see more.</p> <p>Here are two methods, which implement the API methods rtm.test.echo and rtm.test.login, respectively:</p> <pre><code>// Echo should echo the sent values func (c *Client) Echo(p string) (EchoResp, error) { var m echoResp r := Request{&#34;method&#34;: &#34;rtm.test.echo&#34;, &#34;ping&#34;: p} if err := c.doReqURL(c.url(c.urlBase(), r), &amp;m); err != nil { return EchoResp{}, err } return m.RSP, m.RSP.IsOK() } // Login reports what user if any is logged in. func (s *Session) Login() (LoginResp, error) { c := s.parent var m loginResp r := Request{&#34;method&#34;: &#34;rtm.test.login&#34;, &#34;auth_token&#34;: s.Token} if err := c.doReqURL(c.url(c.urlBase(), r), &amp;m); err != nil { return LoginResp{}, err } return m.RSP, m.RSP.IsOK() } </code></pre> <p>Very similar! The second one requires authentication, so it&#39;s a session method. The first one has an argument. Other than that, they look very similar.</p> <p>Here are the response structs and their parent structs:</p> <pre><code>type errorResp struct { Code string `json:&#34;code&#34;` Message string `json:&#34;msg&#34;` } func (e *errorResp) Error() string { return fmt.Sprintf(&#34;code %s, message %s\n&#34;, e.Code, e.Message) } type baseResp struct { Status string `json:&#34;stat&#34;` Error errorResp `json:&#34;err&#34;` } func (b *baseResp) IsOK() error { if b.Status == &#34;fail&#34; { return &amp;b.Error } return nil } // EchoResp is the expected response from rtm.test.echo type EchoResp struct { baseResp Ping string `json:&#34;ping&#34;` } type echoResp struct { RSP EchoResp `json:&#34;rsp&#34;` } // LoginResp is the expected response from rtm.test.login type LoginResp struct { baseResp User struct { ID string `json:&#34;id&#34;` Username string `json:&#34;username&#34;` } `json:&#34;user&#34;` } type loginResp struct { RSP LoginResp `json:&#34;rsp&#34;` } </code></pre> <p>Also very similar, and very repetitive. </p> <p>What I&#39;m looking for from you folks are suggestions on how to reduce duplication in structs and methods, or confirmation that this is as good as it&#39;s going to get. Right now, either would be appreciated!</p> <p>Thank you in advance for your help!</p> <p>Jack.</p> <hr/>**评论:**<br/><br/>EclecticMind: <pre><p>Why include error if there isn&#39;t one? Return an error if it exists, otherwise return the data being requested. Use status codes to help the client react accordingly. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077

237 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传