Skip to content

Instantly share code, notes, and snippets.

@jeremychase
Last active December 17, 2018 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremychase/ee1f054188a81a4f1d4f9d21b0802815 to your computer and use it in GitHub Desktop.
Save jeremychase/ee1f054188a81a4f1d4f9d21b0802815 to your computer and use it in GitHub Desktop.
Example of initializing and sharing package scoped variable
// This package shows a non-concurrent safe way to initialize and
// share a package scoped variable.
package example
// package scoped variable initialized in GetClient().
// its type is not important
var pkgScopedVar []byte
// Client is used for its methods
type Client struct{}
// GetClient initializes pkgScopedVar
func GetClient() (c Client) {
pkgScopedVar = make([]byte, 10) // For Example
return
}
// Example method needs pkgScopedVar
func (c Client) Example() {
_ = pkgScopedVar[:5] // example
}
// SecondExample method also needs pkgScopedVar
func (c Client) SecondExample() {
_ = pkgScopedVar[5:] // example
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment