声明全局指向结构体的指针

huangapple go评论70阅读模式
英文:

Declaring global pointer to structs

问题

我想在全局范围内声明一个指向结构体的指针,以便我可以在包内的其他文件中访问这个指针。我该如何做呢?

详情:
包Y中有一个名为"Cluster"的结构体,还有一些名为NewCluster等的函数。

type Cluster struct {
}
func NewCluster(self *Node, credentials Credentials) *Cluster {
    return &Cluster{
    }
}

现在,在包"X"中,当我尝试像下面这样访问上述cluster时,它能正常工作

cluster := Y.NewCluster(node, credentials)

现在,我想将这个'cluster'声明为一个全局变量,以便我可以在我的"X"包的其他文件中访问它。所以,我尝试了很多种方式来声明它,但都不起作用。我该如何在全局范围内声明它?然后如何在我的"X"包的其他文件中或者同一个文件中(调用相同的NewCluster函数)访问它?

编辑:
我尝试过声明为var cluster Cluster、var *cluster Cluster、var cluster *Cluster等,但都不起作用。

英文:

I want to declare a pointer to a struct globally so that I can access this pointer in other files within my package. How do I do that?

Details:
Package Y has struct named "Cluster" and also some functions named NewCluster etc.

type Cluster struct {
}
func NewCluster(self *Node, credentials Credentials) *Cluster {
	return &Cluster{
	}
}

Now,from package "X" when I tried accessing above cluster as below, it works good

cluster := Y.NewCluster(node, credentials)

Now, I want to declare this 'cluster' as a global variable so that I can access it in other files of my "X" package. So, I am trying to declare it many ways but its not working. How do I declare it globally? and then how do I access it in other files of my "X"package or else in the same file (to invoke same NewCluster function)?

Edit:
I tried declaring as var cluster Cluster, var *cluster Cluster , var cluster *Cluster etc. but nothing works.

答案1

得分: 2

标识符指代在顶层(任何函数之外)声明的常量、类型、变量或函数(但不包括方法)的范围是包块。

Go 语言规范:作用域

因此,在一个包文件的函数之外声明的变量应该在任何其他包文件中都可用。

我认为你只是缺少 Cluster 类型的包名:你需要一个限定标识符。

限定标识符是一个带有包名前缀的标识符。包名和标识符都不能是空白的。

Go 语言规范:限定标识符

Cluster 类型在包 Y 中定义,NewCluster 函数也是如此。当你从包 X 访问 NewCluster 时,你使用了一个限定标识符,通过在函数名前加上包名和一个点号:

cluster := Y.NewCluster(node, credentials)

当你试图从包 X 引用包 Y 中的类型时,你需要使用限定标识符。例如:

var cluster *Y.Cluster
英文:

> The scope of an identifier denoting a constant, type, variable, or
> function (but not method) declared at top level (outside any function)
> is the package block.

Go Language Specification: Scope

So a variable declared in one package file outside of a function should be available in any other package file.

I think all you're missing here is the package name for the Cluster type: you need a qualified identifier.

> A qualified identifier is an identifier qualified with a package name
> prefix. Both the package name and the identifier must not be blank.

Go Language Specification: Qualified Identifiers

The type Cluster is defined in package Y, as is the function NewCluster. When you accessed NewCluster from package X, you used a qualified identifier by prefixing the function name with the package name and a dot:

cluster := Y.NewCluster(node, credentials)

When you're trying to reference a type in package Y from package X, you need to do it with a qualified identifier. For example:

var cluster *Y.Cluster

1: https://golang.org/ref/spec#Declarations_and_scope "Go Language Specification: Scope"
2: https://golang.org/ref/spec#Qualified_identifiers "Go Language Specification: Qualified Identifiers"

huangapple
  • 本文由 发表于 2017年4月7日 12:39:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/43269769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定