go run, add string value definition with space

144 views
Skip to first unread message

Tong Sun

unread,
Nov 9, 2023, 4:26:12 PM11/9/23
to golang-nuts
go run command-line-arguments

  -X definition
        add string value definition of the form importpath.name=value

I used to use this to override my variable values, in the form of

go run -ldflags="-X main.var1=val1 main.var2=val2"

Now, the new problem is that I need to have space in the string value definition. How can I do that please?

I've tried both the following cases:

go run -ldflags="-X main.var1='val 1' main.var2=\"val 2\""

but neither is working. 
Please help


Michel Levieux

unread,
Nov 13, 2023, 4:35:26 AM11/13/23
to Tong Sun, golang-nuts
Hi!

Not an expert at all, but it seems the format is "package-qualified.identifier=value", with value being able to just contain spaces out of the box, so running:

go run -ldflags="-X 'main.MyVar=this is a string'" main.go 

works for me. Note that for parsing the whole thing correctly, the whole equality should be surrounded by quotes (i.e. including the main.MyVar part)

Hope that helps!

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7d1e2b06-dce9-4e46-b20b-fe5a3663038en%40googlegroups.com.

The information transmitted, including attachments, is intended only for the person(s) or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and destroy any copies of this information.

Tong Sun

unread,
Nov 13, 2023, 10:30:32 AM11/13/23
to Michel Levieux, golang-nuts
Yep, works like a charm. thx!
Reply all
Reply to author
Forward
0 new messages