Application auto build versioning

后端 未结 6 833
情话喂你
情话喂你 2020-11-28 00:25

Is it possible to increment a minor version number automatically each time a Go app is compiled?

I would like to set a version number inside my program, with an auto

6条回答
  •  误落风尘
    2020-11-28 00:49

    On Windows OS given the program below

    package main
    
    import "fmt"
    
    var (
        version string
        date    string
    )
    
    func main() {
        fmt.Printf("version=%s, date=%s", version, date)
    }
    

    You can build using

    go build -ldflags "-X main.version=0.0.1 -X main.date=%date:~10,4%-%date:~4,2%-%date:~7,2%T%time:~0,2%:%time:~3,2%:%time:~6,2%"
    

    Date format assumes your environment echo %date% is Fri 07/22/2016 and echo %time% is 16:21:52.88

    Then the output will be: version=0.0.1, date=2016-07-22T16:21:52

提交回复
热议问题