如何访问struct';函数中的实例字段?

如何访问struct';函数中的实例字段?,struct,go,visibility,instance-variables,Struct,Go,Visibility,Instance Variables,假设我有一个图结构,如下所示: type Graph struct { nodes []int adjList map[int][]int } // some methods on the struct // constructor func New() *Graph { g := new(Graph) g.adjList = make(map[int][]int) return g } 现在,我用:aGraph:=new()创建该结构的一个新

假设我有一个
结构,如下所示:

type Graph struct {
    nodes   []int
    adjList map[int][]int
}

// some methods on the struct


// constructor
func New() *Graph {
    g := new(Graph)
    g.adjList = make(map[int][]int)
    return g
}
现在,我用:
aGraph:=new()
创建该结构的一个新实例

如何访问
Graph
struct(
aGraph
)的这个特定实例的字段? 换句话说,如何访问
aGraph
版本的
节点
数组(例如,从另一个顶级函数中访问)

非常感谢您的帮助

这里有一个例子:

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]映射[0:[1990 1991 1992]1:[1890 1891 1892]2:[1790 1791 1792]}

与您在
新功能中所做的相同;使用
。请注意,字段名以小写字母开头,这意味着它们是包的私有名称。您不能直接从另一个包访问它们。为此,让字段名以大写字母开头。这可以根据需要使用。这个问题实际上是完全不相关的,因为我使用的是
go-run
,但我让程序分布在多个文件中,认为
go-run
实际上会包含所有需要的文件,如果我将它指向带有
main()
的文件。事实证明,
go-build
就是所需要的。