Struct tags vs interface with options func

agolangf · · 528 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;ve never liked decorators much, one of the reasons I don&#39;t like python too much, or I am weary of frameworks such as Symfony in PHP. But now I am having a use case where using the struct tags &#34;wouldn&#39;t be too bad&#34;.</p> <p>I feel like I could make an exeption for struct tags, since they are a bit different than function decorators and such, they serve more as metadata vs methods that run with the function. But I also don&#39;t want to let the camel of decorators edge his way into my tent!</p> <p>So I guess there are two methods, #1, with tags:</p> <pre><code>type MyStruct struct { FieldOne string `meta-tag:&#34;this should be bold&#34;` FieldTwo int } </code></pre> <p>Or with an interface:</p> <pre><code>type MetaData interface { whoIsBold() []string } type MyStruct struct { FiledOne string FieldTwo int } func (m * MyStruct) whoIsBold() string { return []string{&#34;FieldOne&#34;} } </code></pre> <p>Any pros &amp; cons of both approaches?</p> <hr/>**评论:**<br/><br/>djherbis: <pre><p>What are you planning to use this for? Depending on your use case, there might be a nicer solution.</p></pre>drink_with_me_to_day: <pre><p>I&#39;m just trying to simplify database crud, without using a ORM, so I just need to store some table related metadata (pk, fk, json, etc).</p></pre>djherbis: <pre><p>Well I still think it depends on how you will be using this data as to which is better. A simple pro/con observation might be that tags will probably take less code to start, but an interface could be more flexible if you design it well.</p></pre>

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

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