How to make a function take any struct, fill it and return it?

xuanbao · · 694 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I got my feet in to go but I&#39;m stuck. I want to make a function that takes any struct as an argument, fill it and then return it or add it (to another struct) as the same type (if I return it as an interface I get that it doesn&#39;t support indexing).</p> <p>Api struct:</p> <pre><code>type api struct { api.url string api.res interface{} // ? } </code></pre> <p>The function looks like this:</p> <pre><code>func (api *API) Get(payload interface{}) { resp, _ := http.Get(api.str) json.NewDecoder(resp.Body).Decode(&amp;payload) api.res = payload // I wanted to return the value but I get that resp.Body is closed defer resp.Body.Close() } </code></pre> <p>Some struct:</p> <pre><code>type somestruct []struct { Text string `json:&#34;text&#34;` } </code></pre> <p>And I use it like this:</p> <pre><code>JsonToStruct := somestruct{} someapi.Get(JsonToStruct) someapi.res[0] // type interface {} does not support indexing </code></pre> <p>So is it possible to make a method that is able to take any struct and return it with additional data?</p> <p>The problem is that I have multiple different structs and I don&#39;t want to hardcode the structs.</p> <p>I have the code on github, and I would appreciate any help. <a href="https://github.com/taosx/worder/" rel="nofollow">https://github.com/taosx/worder/</a></p> <hr/>**评论:**<br/><br/>pappogeomys: <pre><p>Fix the type declaration for <code>def</code>. You have a slice of anonymous structs, but you want to end up with a slice of <code>def</code> structs:</p> <pre><code>type def struct { </code></pre> <p>You create a slice with <code>make</code>, not <code>new</code> (but you don&#39;t need to make it since you&#39;re gong to let the json package do that for you)</p> <pre><code>var json []def w.Get(&amp;json) </code></pre> <p>Then don&#39;t ever use pointer to an interface, so decode with </p> <pre><code>err = json.NewDecoder(resp.Body).Decode(payload) </code></pre></pre>sh4rk1z: <pre><p>In addition to the changes mentioned by you and using typecasting (*[]def) now I can access the struct even if it looks a bit weird. Thank you!</p></pre>pappogeomys: <pre><p>What part looks weird? (BTW, that&#39;s a <a href="https://golang.org/ref/spec#Type_assertions" rel="nofollow">&#34;type assertion&#34;</a>)</p></pre>sh4rk1z: <pre><p>I use w.Get in a function (Definition) and I return it&#39;s result, but I can&#39;t access it directly so:</p> <pre><code>*Definition(&#34;car&#34;).(*[]def) </code></pre> <p>You&#39;re right it&#39;s called type assertion.</p> <p>func Definition: <a href="https://github.com/taosx/worder/blob/master/wordnikapi.go#L22" rel="nofollow">https://github.com/taosx/worder/blob/master/wordnikapi.go#L22</a></p> <p>Type assertion: <a href="https://github.com/taosx/worder/blob/master/worder.go#L25" rel="nofollow">https://github.com/taosx/worder/blob/master/worder.go#L25</a></p></pre>christopherhesse: <pre><p>Why doesn&#39;t func Definition just return []def instead of interface{}?</p></pre>

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

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