Last active
December 17, 2018 16:24
-
-
Save jeremychase/ee1f054188a81a4f1d4f9d21b0802815 to your computer and use it in GitHub Desktop.
Example of initializing and sharing package scoped variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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