假设我有一个Graph结构,如下所示:type Graph struct { nodes []int adjList map[int][]int}// some methods on the struct// constructorfunc New() *Graph { g := new(Graph) g.adjList = make(map[int][]int) return g}现在,我创建了结构的新实例,有:aGraph := New()。如何访问Graph结构 ( aGraph)的这个特定实例的字段?换句话说,我如何访问aGraph's 版本的nodes数组(例如,从另一个顶级函数中)?非常感谢任何帮助!
1 回答
慕后森
TA贡献1802条经验 获得超5个赞
这是一个例子:
package main
import (
"fmt"
)
// example struct
type Graph struct {
nodes []int
adjList map[int][]int
}
func New() *Graph {
g := new(Graph)
g.adjList = make(map[int][]int)
return g
}
func main() {
aGraph := New()
aGraph.nodes = []int {1,2,3}
aGraph.adjList[0] = []int{1990,1991,1992}
aGraph.adjList[1] = []int{1890,1891,1892}
aGraph.adjList[2] = []int{1890,1891,1892}
fmt.Println(aGraph)
}
输出:&{[1 2 3 4 5] map[0:[1990 1991 1992] 1:[1890 1891 1892] 2:[1790 1791 1792]]}
- 1 回答
- 0 关注
- 167 浏览
添加回答
举报
0/150
提交
取消