<p>I got my feet in to go but I'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'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(&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:"text"`
}
</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'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't need to make it since you're gong to let the json package do that for you)</p>
<pre><code>var json []def
w.Get(&json)
</code></pre>
<p>Then don'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's a <a href="https://golang.org/ref/spec#Type_assertions" rel="nofollow">"type assertion"</a>)</p></pre>sh4rk1z: <pre><p>I use w.Get in a function (Definition) and I return it's result, but I can't access it directly so:</p>
<pre><code>*Definition("car").(*[]def)
</code></pre>
<p>You're right it'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't func Definition just return []def instead of interface{}?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传