douwang9650 2015-09-08 22:30
浏览 113
已采纳

使用go build时如何在链接标志的值字符串中包含空格

Here is test code m.go:

package main

var version string

func main() {
    println("ver = ", version)
}

If I compile and link with go 1.5:

go tool compile m.go
go tool link -o m -X main.version="abc 123" m.o

Works fine.

But if I use build command with go 1.5:

go build -o m -ldflags '-X main.version="abc 123"' m.go

It will show help message, which means something wrong

If I change to 1.4 syntax:

go build -o m -ldflags '-X main.version "abc 123"' m.go

It works except a warning message:

link: warning: option -X main.version abc 123 may not work in future releases; use -X main.version=abc 123

If it has no space in parameter value, works fine:

go build -o m -ldflags '-X main.version=abc123' m.go

Because compile and link works fine, So I think it is not link part issue. I compared go1.4 and go 1.5 source code of build, for ldflags part, looks nothing changed. Of cause I can use some spacial char to replace space then in program to change it back, but why? Is there something I missed? What is the right syntax to use -ldflags ? Thanks

  • 写回答

1条回答 默认 最新

  • douzhao9608 2015-09-08 22:35
    关注

    From the documentation:

    Note that before Go 1.5 this option took two separate arguments.
    Now it takes one argument split on the first = sign.

    Enclose the entire argument in quotes:

    go build -o m -ldflags '-X "main.version=abc 123"' m.go
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?