Last active
June 30, 2016 11:59
-
-
Save serverhorror/2fc331df7ed71ed24c10b823392967e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Go! Why don't you use my version? | |
| ## WRONG: go run -ldflags="-X commonFlagTest.version='my-uber-version'" cmd/flagtest/main.go -version | |
| go run -ldflags="-X github.com/serverhorror/commonFlagTest.versionString='$(TZ=UTC date '+%FT%T.%N%:z')'" cmd/flagtest/main.go -version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| "github.com/serverhorror/commonFlagTest" | |
| ) | |
| var ( | |
| localFlag = flag.Bool("local", false, "Flag local to the main.go file") | |
| ) | |
| func main() { | |
| commonFlagTest.Main() | |
| if *localFlag { | |
| fmt.Println("Local Flag") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package commonFlagTest | |
| import ( | |
| "flag" | |
| "fmt" | |
| "os" | |
| "runtime" | |
| ) | |
| var ( | |
| version string | |
| versionFlag = flag.Bool("version", false, "Print version and exit") | |
| ) | |
| func Main() { | |
| flag.Parse() | |
| if *versionFlag { | |
| PrintVersion() | |
| } | |
| } | |
| func PrintVersion() { | |
| fmt.Printf("Version: %#q\n", version) | |
| fmt.Printf("Runtime: %#q\n", runtime.Version()) | |
| os.Exit(1) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment