On Wed, Nov 8, 2023 at 1:35 PM Stephen Illingworth < stephen.illingwo...@gmail.com> wrote: > > Hello, > > I'm trying to use the -X ldflag to set a string at compile time. I can do this successfully if the string is in the main package but it does not work if the string is in a subpackage. > > For illustration purposes, I have prepared a simple test project > > https://github.com/JetSetIlly/ldflags_X_test > > The build.sh file contains the build string. It contains one useful line, which I'll repeat it here: > > go build -ldflags "-X 'main.A=foo' -X 'sub.B=bar'" > > When built, the main function will print out the A string correctly but the B string is unset. > > What am I doing wrong?
The second -X flag does not have the documented form, see https://pkg.go.dev/cmd/link. jnml@3900x:~/src/x/ldflags_X_test$ ls -l total 16 -rwxr-xr-x 1 jnml jnml 101 Nov 8 13:47 build.sh -rw-r--r-- 1 jnml jnml 55 Nov 8 13:47 go.mod -rw-r--r-- 1 jnml jnml 144 Nov 8 13:47 main.go drwxr-xr-x 2 jnml jnml 4096 Nov 8 13:47 sub jnml@3900x:~/src/x/ldflags_X_test$ git diff diff --git a/build.sh b/build.sh index db86e35..0185e5e 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/bash -go build -ldflags "-X 'main.A=foo' -X 'sub.B=bar'" +go build -ldflags "-X 'main.A=foo' -X ' github.com/JetSetIlly/ldflags_X_test/sub.B=bar'" jnml@3900x:~/src/x/ldflags_X_test$ ./build.sh && ./ldflags_X_test foo bar jnml@3900x:~/src/x/ldflags_X_test$ -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA40n-XP7%2BiTS_aCR88%2Buuzi5Xo7ypHJeNDkczvHJUtzpjrE3Q%40mail.gmail.com.