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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库