Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. $GOROOT/src/sample/app/main.go
  2. $GOROOT/src/sample/app/aflag.go
  3. $GOROOT/src/sample/app/zflag.go
  4.  
  5. package main
  6.  
  7. import "fmt"
  8.  
  9. type appConfig struct {
  10. Debug bool
  11. Mode string
  12. }
  13.  
  14. var config *appConfig
  15.  
  16. func init() {
  17. fmt.Println("main::init()");
  18. if config == nil {
  19. fmt.Println("main::set_config()");
  20. config = &appConfig{
  21. Debug: true,
  22. Mode: "Development",
  23. }
  24. }
  25. }
  26. func main() {
  27. fmt.Println("Current config is:", config)
  28. }
  29.  
  30. // +build aflag
  31.  
  32. package main
  33.  
  34. import "fmt"
  35.  
  36. func init() {
  37. fmt.Println("aflag::init()");
  38. if config == nil {
  39. fmt.Println("aflag::set_config()");
  40. config = &appConfig{
  41. Debug: false,
  42. Mode: "Production",
  43. }
  44. }
  45. }
  46.  
  47. // +build zflag
  48.  
  49. package main
  50.  
  51. import "fmt"
  52.  
  53. func init() {
  54. fmt.Println("zflag::init()");
  55. if config == nil {
  56. fmt.Println("zflag::set_config()");
  57. config = &appConfig{
  58. Debug: false,
  59. Mode: "Testing",
  60. }
  61. }
  62. }
  63.  
  64. $ go build -o bin/main sample/app
  65. $ go build -tags aflag -o bin/main-a sample/app
  66. $ go build -tags zflag -o bin/main-z sample/app
  67.  
  68. $ ./bin/main
  69. main::init()
  70. main::set_config()
  71. Current config is: &{true Development}
  72.  
  73. $ /bin/main-a
  74. aflag::init()
  75. aflag::set_config()
  76. main::init()
  77. Current config is: &{false Production}
  78.  
  79. $ /bin/main-z
  80. main::init()
  81. main::set_config()
  82. zflag::init()
  83. Current config is: &{true Development}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement