On this page
article
Golang
Setting up a logger
log.Printf("message %s", "Hello World")
Network requests
Set Headers
req.Header = http.Header{
"Host": {"www.host.com"},
"Content-Type": {"application/json"},
"Authorization": {"Bearer Token"},
}
Can also do this, but note that Host header cannot be set this way
req.Header.Set("Key", "Value")
Network Request
Flags
Check to see if a flag was changed or not
modelFlagModified := flag.Lookup("model").Changed
Build Args
main.go:
var inputVariable = "defaultValue"
bash:
go build -ldflags "-X main.inputValue=setValue" -o myapp
-X importpath.name=value
Set the value of the string variable in importpath named name to value.
This is only effective if the variable is declared in the source code either uninitialized
or initialized to a constant string expression. -X will not work if the initializer makes
a function call or refers to other variables.
Note that before Go 1.5 this option took two separate arguments.
Also note, to add to another package, you have to specify it by full path. So if the package “cmd” is located at app/cmd, you would set the build args like this:
module_name="demo-cli"
BUILD_ARGS+=("-X" "'$module_name/app/cmd.binaryName=$app_name'")
BUILD_ARGS+=("-X" "'$module_name/app/cmd.gitVersion=$git_version'")
BUILD_ARGS+=("-X" "'$module_name/app/cmd.buildTime=$build_time'")