<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: "first header", Data: --- }
| ... nested Node's
|
[1] Type: header,
| Data: Header{ Str: "Second header", Data: --- }
| ... nested Node's
|
[2] Type: text
| Data: Text{ Str: "text" }
|
....
</code></pre>
<p>It can be done? How create and access the structure?</p>
<hr/>**评论:**<br/><br/>Akkifokkusu: <pre><p>I'm not sure exactly what your question is, but perhaps you'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 "objects" 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>'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'm going to play with the code</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传