Skip to content

Instantly share code, notes, and snippets.

@linnv
Last active September 2, 2017 23:34
Show Gist options
  • Select an option

  • Save linnv/70b44b2b1326f8f27cfe337c24e687df to your computer and use it in GitHub Desktop.

Select an option

Save linnv/70b44b2b1326f8f27cfe337c24e687df to your computer and use it in GitHub Desktop.

Revisions

  1. linnv renamed this gist Sep 2, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. linnv revised this gist Aug 23, 2017. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -32,13 +32,11 @@ func VersionGuide() {
    upperV := strings.ToUpper(v)
    for _, ver := range versionList {
    if ver == upperV {
    // logx.Debugf("Version: %+v\n", Version)
    os.Stdout.Write([]byte("Version: " + Version + "\n"))
    }
    }
    for _, ver := range buildTimeList {
    if ver == upperV {
    // logx.Debugf("BuildTime: %+v\n", BuildTime)
    os.Stdout.Write([]byte("BuildTime: " + BuildTime + "\n"))
    }
    }
    @@ -49,4 +47,4 @@ func main() {
    flag.Parse()

    VersionGuide()
    }
    }
  3. linnv created this gist Aug 23, 2017.
    52 changes: 52 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    package main

    import (
    "flag"
    "os"
    "strings"
    )

    var (
    Version = "1.0.0"
    BuildTime = "2017-08-23_11:44:38"
    )

    //go build -ldflags "-X 'main.Version=`git rev-parse HEAD`' -X 'main.BuildTime=`date '+%Y-%m-%d_%H:%M:%S'`' " main.go

    func VersionGuide() {
    if !flag.Parsed() {
    os.Stderr.Write([]byte("ERROR: do flag.Parse() first!"))
    return
    }

    args := flag.Args()
    versionList := [...]string{
    "V", "-V", "--V",
    "VERSION", "-VERSION", "--VERSION",
    }
    buildTimeList := [...]string{
    "T", "-T", "--T",
    "BUILDTIME", "-BUILDTIME", "--BUILDTIME",
    }
    for _, v := range args {
    upperV := strings.ToUpper(v)
    for _, ver := range versionList {
    if ver == upperV {
    // logx.Debugf("Version: %+v\n", Version)
    os.Stdout.Write([]byte("Version: " + Version + "\n"))
    }
    }
    for _, ver := range buildTimeList {
    if ver == upperV {
    // logx.Debugf("BuildTime: %+v\n", BuildTime)
    os.Stdout.Write([]byte("BuildTime: " + BuildTime + "\n"))
    }
    }
    }
    }

    func main() {
    flag.Parse()

    VersionGuide()
    }