<p>I've never liked decorators much, one of the reasons I don'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 "wouldn't be too bad".</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'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:"this should be bold"`
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{"FieldOne"}
}
</code></pre>
<p>Any pros & 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'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
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传