我创建了一个具有列表的结构,其中包含对相同结构类型的值的引用。我是 Go 语言的新手,无法找到一种方法来访问自动解析为上述结构类型的值。在 java 中是这样的:
class Node{
String value ;
String key;
List<Node> children = new ArrayList<Node>();
public Node(String key, value) {
// rest of the code follows
}
}
class AccessNode {
public static void main(String args[]) {
Node node = new Node("key", "value");
// The values automatically resolve to type Node.
for(Node node : node.children) {
// do something
}
}
节点定义如下:
type Node struct {
key string
value string
isword bool
childern *list.List // This is essentially a list of Node}
// Next two functions are a copy of implementation in list package.
func (n *Node) Init() *Node {
n.isword = false
n.childern = list.New()
return n}
func New() *Node {
return new(Node).Init()}
现在我遍历子节点并在任何时候返回,我得到一个节点,它的值与要比较的字符串部分匹配。
func countMatchingChars(key string, node *Node) (int, *Node) {
count := 0
for e := node.childern.Front(); e != nil; e = e.Next() {
// Getting error when accessing e.Value.key
if c := MatchCount(e.Value.key, key); c > 0 {
return c, e.Value
}
}}
我收到以下错误
./trie.go:53: e.Value.key undefined (type interface {} has no field or method key)
./trie.go:54: cannot use e.Value (type interface {}) as type *Node in return argument: need type assertion