Go tool link: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
=Options=
=Options=
When invoked by <code>go build</code>, all of the following options can be passed from <code>go build</code> command line with the <code>-ldflags</code> option. For syntax, see: {{Internal|Go_build#-ldflags|<tt>go build -ldflags</tt>}}
When invoked by <code>go build</code>, all of the following options can be passed from <code>go build</code> command line with the <code>-ldflags</code> option. For syntax, see: {{Internal|Go_build#-ldflags|<tt>go build -ldflags</tt>}}
==<tt>-X importpath.name=value</tt>==
Set the value of the string variable in [[Go_Packages#Import_Path|import path]] named name to value. This option is only effective if the variable is declared in the source code either uninitialized or initialized to a constant string expression. <code>-X</code> will not work if the initializer makes a function call or refers to other variables.
<syntaxhighlight lang='bash'>
go tool link -X 'example.com/myapp/internal/version.Version=1.2.0' [...]
</syntaxhighlight>

Latest revision as of 22:43, 1 March 2024

External

Internal

Overview

The linker is typically invoked as go tool link or as part of the go build command. It reads the Go archive or object for a package main, along with its dependencies, and combines them into an executable binary.

Options

When invoked by go build, all of the following options can be passed from go build command line with the -ldflags option. For syntax, see:

go build -ldflags

-X importpath.name=value

Set the value of the string variable in import path named name to value. This option is only effective if the variable is declared in the source code either uninitialized or initialized to a constant string expression. -X will not work if the initializer makes a function call or refers to other variables.

go tool link -X 'example.com/myapp/internal/version.Version=1.2.0' [...]