-ldflags -X

170 views
Skip to first unread message

Eric Brown

unread,
Jan 26, 2017, 10:58:49 PM1/26/17
to golang-nuts
Using -ldflags -X, is there a way to set the internal value as the output of an executable console file?

I've seen examples like this:

"-X main.githash=`git rev-parse HEAD`" (which would put the result of the git command w/ flags into it)

however, I just can't get it to work (if it's even possible).

The file I use is called setVersion.exe and does all the formatting, etc. for me (which includes connecting to a ntp atomic time server for the time stamp, etc.).

What I'm using is (setVersion being included in the PATH environment):

go build -ldflags "-X 'main.versionBuild=setVersion'"

I've tried putting the quotations in different places, etc.  Nothing I try is working.

Eric Brown

unread,
Jan 27, 2017, 12:57:19 AM1/27/17
to golang-nuts
Got it to work as intended under Linux; however, compiling on Windows is a no-go.  Perhaps I have to create a script, pass the output to a variable, and insert the variable into the build command?  Hopefully not, so if anybody has any suggestions... it would be highly appreciated.  Thanks!

Arafath M

unread,
Jan 27, 2017, 1:43:01 AM1/27/17
to Eric Brown, golang-nuts
Hi Eric,

Under Windows, create a bat file with following contents. It uses latest tag from git repo as version.

echo Rebuilding App...
for /f %%i in ('git describe --tags ') do set version=%%i
go install -a -v -ldflags "-X main.versionBuild=%version%"



Sincerely,
Arafath M

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Eric Brown

unread,
Jan 27, 2017, 2:02:38 AM1/27/17
to golang-nuts, edb...@gmail.com
Arafath, that's kind of what I did (except I replaced the git command with my setversion.exe). It worked as expected... thank you!

Sincerely,
Arafath M

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages