golang struct json tags and gin output

blov · · 642 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a struct that is storing values from an API, the json tags are being used to match what the API is returning.</p> <p>within code, if I print the struct, I see my field names. However, when I send the struct to gin, it is using the tag names.</p> <p>I am tryng to figure out how to either use the struct field names in the original struct, or copy the value of the struct to similar struct, but use json tags to give a custom name.</p> <p><strong>example:</strong></p> <pre><code>type ComputeResponse struct { Endpoints []ComputeObject } type ComputeObject struct { Hostname string `json:&#34;hostname&#34;` LogicalEnvironment string `json:&#34;lgcl_env&#34;` } </code></pre> <p>If I print the values of ComputeResponse.Endpoints I will see something like this:</p> <pre><code>[{Hostname:test LogicalEnvironment:test] </code></pre> <p>but in the browser or curl response I get:</p> <pre><code>{ &#34;hostname&#34;: &#34;test&#34;, &#34;lgcl_env&#34;: &#34;test&#34; } </code></pre> <p>I am pretty new to go. Any suggestions would be appreciated.</p> <hr/>**评论:**<br/><br/>kpurdon: <pre><p><a href="https://play.golang.org/p/koMxQK9-pR" rel="nofollow">https://play.golang.org/p/koMxQK9-pR</a></p></pre>

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

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