go - Declaring global pointer to structs -
i want declare pointer struct globally can access pointer in other files within package. how do that?
details: package y has struct named "cluster" , functions named newcluster etc.
type cluster struct { } func newcluster(self *node, credentials credentials) *cluster { return &cluster{ } }
now,from package "x" when tried accessing above cluster below, works good
cluster := y.newcluster(node, credentials)
now, want declare 'cluster' global variable can access in other files of "x" package. so, trying declare many ways not working. how declare globally? , how access in other files of "x"package or else in same file (to invoke same newcluster function)?
edit: tried declaring var cluster cluster, var *cluster cluster , var cluster *cluster etc. nothing works.
the scope of identifier denoting constant, type, variable, or function (but not method) declared @ top level (outside function) package block.
go language specification: scope
so variable declared in 1 package file outside of function should available in other package file.
i think you're missing here package name cluster type: need qualified identifier.
a qualified identifier identifier qualified package name prefix. both package name , identifier must not blank.
go language specification: qualified identifiers
the type cluster defined in package y, function newcluster. when accessed newcluster package x, used qualified identifier prefixing function name package name , dot:
cluster := y.newcluster(node, credentials)
when you're trying reference type in package y package x, need qualified identifier. example:
var cluster *y.cluster
Comments
Post a Comment