I have seen the following command to set build version
go build -ldflags "-X main.minversion `date -u +.%Y%m%d%.H%M%S`" service.go
but I cant get the timestamp part to work on Windows !
I am using go 1.5 with the following arguments...
build -i -ldflags "-X main.SERVER_NAME=MFFP -X main.VERSION=1.0.0 -X main.BUILD_DATE=`date -u +.%Y%m%d%.H%M%S`"
The error is with the date part
Is this error system related? Anyone tried this on Windows?
Thanks!
The command on *nix systems is using backtick command substitution. date
is a built-in on POSIX, and anything inside `
s is interpreted as a shell command and is replaced by its value.
cmd.exe
doesn't do command substitution inside backticks at all, There's some ways to accomplish the same thing using for /F delims=""
, aka the most powerful command in Windows shell, but it's pretty hacky. Maybe use powershell instead, which supports the $(command)
syntax?
PS C:\users\adsmith> go build -i -ldflags "-X main.SERVER_NAME=MFFP -X main.VERSION=1.0.0 -X main.BUILD_DATE=$(Get-Date -uformat .%Y%m%d%.H%M%S)"
You might have to play with the format -- I'm not sure what that's supposed to look like on POSIX. This outputs:
.20160107.111641
// Jan 7th 2016, 11:16:41