(question) access and create nodes in structure with slices of type interface

xuanbao · · 342 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Suppose I have these structures</p> <pre><code>type Node struct { Type uint8 Data interface{} } type Header struct { Str string Data []Node } type Command struct { Type string Params string Arg string Body []Node } type Text struct { str string } </code></pre> <p>and I want create something like</p> <pre><code>Document []Node \ [0] Type: header, | Data: Header{ Str: &#34;first header&#34;, Data: --- } | ... nested Node&#39;s | [1] Type: header, | Data: Header{ Str: &#34;Second header&#34;, Data: --- } | ... nested Node&#39;s | [2] Type: text | Data: Text{ Str: &#34;text&#34; } | .... </code></pre> <p>It can be done? How create and access the structure?</p> <hr/>**评论:**<br/><br/>Akkifokkusu: <pre><p>I&#39;m not sure exactly what your question is, but perhaps you&#39;re looking for something like: <a href="https://play.golang.org/p/p3X1H050HO" rel="nofollow">https://play.golang.org/p/p3X1H050HO</a></p></pre>nasciiboy: <pre><p>thank you, the question is, how would access the information of such nodes? and how would create the internal nodes?</p> <p>the point is to have a structure to store the headers of a document and the respective content of each element</p> <p>for the moment change the focus to &#34;objects&#34; and object structures <a href="https://play.golang.org/p/MRUvFBT8CZ" rel="nofollow">https://play.golang.org/p/MRUvFBT8CZ</a></p></pre>ChristophBerger: <pre><p><a href="/u/Akkifokkusu" rel="nofollow">/u/Akkifokkusu</a>&#39;s code shows how to create internal nodes. To access the internal nodes, you would need a type switch, since Data is an empty interface: <a href="https://play.golang.org/p/8tJ_882lUs" rel="nofollow">https://play.golang.org/p/8tJ_882lUs</a></p></pre>nasciiboy: <pre><p>I&#39;m going to play with the code</p></pre>

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

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