cfsvcenv
is a Go package for binding Cloud Foundry service credentials
(including user-provided services) to environment variables.
Your Go program uses Cloud Foundry services to provide configuration to it.
Cloud Foundry does this by injecting the configuration into the VCAP_SERVICES
environment variable.
However, you want to keep your program CF-agnostic, and you also want to keep development simple
cfsvcenv
binds the CF service credentials into the OS's environment so that
your Go program does not need any special treatment when used within CF.
You cannot define environment variables in the global scope because these have
not yet been bound. This is because the init()
function that does the binding
has not yet run.
For example this won't work:
package main
import (
"fmt"
"os"
)
var serviceAPIKey = os.Getenv("SERVICE_API_KEY")
func main() {
fmt.Println(serviceAPIKey) // Will be empty
}
go test -race ./...