Skip to content

Instantly share code, notes, and snippets.

@ashcrow
Created November 7, 2017 16:55
Show Gist options
  • Save ashcrow/2a759dca9ce77099bfb2054a4fba42f8 to your computer and use it in GitHub Desktop.
Save ashcrow/2a759dca9ce77099bfb2054a4fba42f8 to your computer and use it in GitHub Desktop.
Package level variable
// ...
const port = 8080
const path = "hash"
const key = "JAVA_SERVICE_HOST"
// hostName houses the package level hostname as a single instance
var hostName string
// SetHostname allows external packages to set the hostname
func SetHostname(name string) {
hostName = name
}
// lookupHostname attempts to lookup the hostname if the variable is not set.
// If hostName is already set it will use that instance
func lookupHostname() string {
// If the hostName is empty ...
if hostName == "" {
// then look it up via environment variable
hostname, ok := os.LookupEnv(key)
if !ok {
// ... and kill execution if we can't get the variable
log.Logger.Panicf("The environment variable %s must be set!", key)
}
}
uri := fmt.Sprintf("http://%v:%v/%v", hostname, port, path)
log.Logger.Infof("Using Java Hash Service at: %s", uri)
return uri
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment