golang struct json tags and gin output

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