Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $GOROOT/src/sample/app/main.go
- $GOROOT/src/sample/app/aflag.go
- $GOROOT/src/sample/app/zflag.go
- package main
- import "fmt"
- type appConfig struct {
- Debug bool
- Mode string
- }
- var config *appConfig
- func init() {
- fmt.Println("main::init()");
- if config == nil {
- fmt.Println("main::set_config()");
- config = &appConfig{
- Debug: true,
- Mode: "Development",
- }
- }
- }
- func main() {
- fmt.Println("Current config is:", config)
- }
- // +build aflag
- package main
- import "fmt"
- func init() {
- fmt.Println("aflag::init()");
- if config == nil {
- fmt.Println("aflag::set_config()");
- config = &appConfig{
- Debug: false,
- Mode: "Production",
- }
- }
- }
- // +build zflag
- package main
- import "fmt"
- func init() {
- fmt.Println("zflag::init()");
- if config == nil {
- fmt.Println("zflag::set_config()");
- config = &appConfig{
- Debug: false,
- Mode: "Testing",
- }
- }
- }
- $ go build -o bin/main sample/app
- $ go build -tags aflag -o bin/main-a sample/app
- $ go build -tags zflag -o bin/main-z sample/app
- $ ./bin/main
- main::init()
- main::set_config()
- Current config is: &{true Development}
- $ /bin/main-a
- aflag::init()
- aflag::set_config()
- main::init()
- Current config is: &{false Production}
- $ /bin/main-z
- main::init()
- main::set_config()
- zflag::init()
- Current config is: &{true Development}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement