duan0065626385 2014-09-12 16:31
浏览 51
已采纳

Go的链接器可以覆盖初始化的变量吗

From ld's docs:

-X symbol value

Set the value of an otherwise uninitialized string variable. The symbol name should be of the form importpath.name, as displayed in the symbol table printed by "go tool nm".

So this is pretty cool. It allows you to do stuff like this:

package main

import "fmt"

var version string

func main() {
    fmt.Println(version)
}

Compile with: go build -ldflags '-X main.version 42' ...

I have two question about his feature. First of all it also works for initialized strings (e.g. var version = "bad build") even though the documentation specifically says "otherwise uninitialized string variable".

The seconds question is about spaces. My Makefile contains the following lines:

GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null)

LDFLAGS := '-X main.version "$(GIT_BRANCH) $(GIT_COMMIT)"'

The documentation for the go command says:

-ldflags 'flag list'

So they're using single quotes for all linker flags. But what about a string containing spaces as symbol for the -X flag? The double-quotes work just fine, so do escaped single-quotes btw., I'm just not sure I can rely on all that to work consistently given the docs not mentioning any of it.

Clarification of the first question:

Go zero-initializes all vars.

The documentation says: -X symbol value Set the value of an otherwise uninitialized string variable [...].

Does this mean:

var foo string // only this one?
var bar = "bar" // or this one too, maybe
  • 写回答

1条回答 默认 最新

  • dongsu7049 2014-09-12 17:45
    关注

    The quotes are handled by the shell (or make), so yes it's consistent.

    The calling program populates go's args.

    //edit

    To use a default version you can use something like this:

    var version string
    
    func init() {
        if len(version) == 0 {
            version = "master"
        }
    }
    

    //edit 2

    From the spec:

    When memory is allocated to store a value, either through a declaration or a call of make or new, and no explicit initialization is provided, the memory is given a default initialization. Each element of such a value is set to the zero value for its type: false for booleans, 0 for integers, 0.0 for floats, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟