O'Reilly logo

Go: Design Patterns for Real-World Projects by Mat Ryer, Mario Castro Contreras, Vladimir Vivien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Package visibility

Regardless of the number of source files declared to be part of a package, all source code elements (types, variables, constants, and functions), declared at a package level, share a common scope. Therefore, the compiler will not allow an element identifier to be re-declared more than once in the entire package. Let us use the following code snippets to illustrate this point, assuming both source files are part of the same package $GOPATH/src/foo:

package foo   
   
var (   
  bar int = 12   
)   
   
func qux () {   
  bar += bar   
}   

foo/file1.go

package foo   
   
var bar struct{   
  x, y int   
}   
   
func quux() {   
  bar = bar * bar   
}   

foo/file2.go

Illegal variable identifier re-declaration

Although they are in two separate files, the declaration of variables with identifier ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required