Question regarding API packages and attribute types

xuanbao · · 411 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m working with an API that has a bunch of attributes it can spit out relating to the content. For example: Name of a file, size of the file, UUID of the content, etc etc. There&#39;s about 30. </p> <p>My first instinct is to create a type for each one, so I can have packagename.Size and make it sortable, etc etc, but this fills the package with a ton of different custom types and looks like garbage. </p> <p>What would be a better way to do this?</p> <hr/>**评论:**<br/><br/>Ainar-G: <pre><p>If all those attributes are strings, just use <a href="https://golang.org/pkg/sort/#Strings" rel="nofollow"><code>sort.Strings</code></a> to sort them.</p></pre>inhies: <pre><p>In the examples given, yes, but there a plenty of ints and time.Times as well.</p> <p>Basically I&#39;m trying to avoid having a generic Attributes type where its just a struct with Name and Value fields, both of the string type.</p></pre>jasonrichardsmith: <pre><p>From my understand custom types don&#39;t cost anything.</p> <p>Making custom types do make it easier to act upon that struct in a specific fashion.</p> <p>You also have the os FileInfo and File types already built.</p> <p>I would personally write what was the most idiomatic for other developers.</p></pre>

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

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