Interface examples and tips?

agolangf · · 534 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I come from a C/python background. One of the big things that I struggle with in Go is designing my applications around interfaces and taking advantage of their power. I am currently trying to design a CRUD web app. Any examples or tips on desiging with interfaces in this context?</p> <hr/>**评论:**<br/><br/>jerf: <pre><p>First, there&#39;s no virtue in using them where they aren&#39;t necessary.</p> <p>Second, keep them minimal. There&#39;s nothing wrong with one-method interfaces. If there&#39;s two methods &#34;sort of&#34; related, it can still be better to have two interfaces and a second one:</p> <pre><code>type Composed interface { FirstInterface SecondInterface } </code></pre> <p>One of the best ways to learn interfaces is to be sure to be testing your code as you go. What you&#39;ll find is that you may have something that looks like this:</p> <pre><code>func DoSomething(f *os.File) error { ... // only Read()s from f ... } </code></pre> <p>When you go to test that, you&#39;ll sigh in exasperation about how you need to create a file just so you can test this function. (Free bonus hint: If you do really need to do that, see <code>ioutil.TempDir</code> or <code>ioutil.TempFile</code>.) But if you <em>only</em> call Read, you really want an <code>io.Reader</code> there, which can then be easily satisfied in your test with</p> <pre><code>r := bytes.NewBuffer([]byte(&#34;Your Test Content Here&#34;)) </code></pre> <p>In general, this is a really powerful use of interfaces: Where ever you take in a rich and complicated object like <code>*os.File</code>, but only use a small fraction of it, use an interface to declare that small fraction, and watch your test code grow in power even as it shrinks in size.</p> <p>Also, this is not a general thing about interfaces, but prefer <code>io.Reader</code> and <code>io.Writer</code> to strings where possible. It&#39;s a huge code smell to take a string in and immediately wrap a <code>byte.Buffer</code> around it to turn it back into a reader. It&#39;s a code smell, though arguably less smelly, to take in an io.Reader and immediately <code>io.ReadAll</code> on it.</p></pre>sindbis: <pre><p>This was really helpful thanks. I am thinking about just abstracting out the database interactions into interfaces. </p></pre>varun06: <pre><p>I will say start with Go Std library. It has lots of interfaces. Also you can look at some web frameworks such as Martini etc. and you can find interface uses there too.</p></pre>QThellimist: <pre><p>Checkout Stringer, sort.Interface in builtin libraries. there are blogs explaining interfaces and giving examples.</p></pre>kromem: <pre><p>For me, Go&#39;s interfaces became really awesome when I started defining them at the receiver instead of the provider. </p> <p>Go does interfaces different from others in that <strong>anything</strong> that has the methods to satisfy the interface will. </p> <p>This makes it trivially easy to write decoupled code by programming to an interface you define based on what you actually want/need to interact with. </p> <p>My code was much easier to test after doing this, and with much looser coupling. </p> <p>As well, for CRUD code, I ended up taking the modelq package and modified it with my own template to generate my CRUD code. I went with reflection the first time around and was not a fan. Code generation using the actual database schema as the source of truth has been much cooler.</p></pre>

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

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