Go: Difference between revisions

DHCP (talk | contribs)
m relace non-functional `` monospace notation with <code> tags
DHCP (talk | contribs)
m use monospace formatting where applicable, add more links, update link to the Go homepage
 
Line 1: Line 1:
[https://golang.org/ Go] is a statically-typed language with syntax loosely derived from that of C, adding garbage collected memory management, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.
[https://go.dev/ Go] is a statically-typed language with syntax loosely derived from that of C, adding garbage collected memory management, type safety, some dynamic-typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library.


== buildGoModule ==
== buildGoModule ==
Line 92: Line 92:


== Using cgo on NixOS ==
== Using cgo on NixOS ==
On NixOS, include files and libraries aren't kept in a system-wide search path. If a Go program uses cgo and attempts to include C header files, or link against libraries, compilation is likely to fail.
On NixOS, include files and libraries aren't kept in a system-wide search path. If a Go program uses [https://go.dev/wiki/cgo cgo] and attempts to include [[C]] header files, or link against libraries, compilation is likely to fail.


In order to expose header files and libraries in environment variable search paths, <tt>nix-shell</tt> can be used to enter an environment which provides the requested development dependencies.
In order to expose header files and libraries in environment variable search paths, <tt>nix-shell</tt> can be used to enter an environment which provides the requested development dependencies.


For example, suppose a Go program includes <tt>&lt;sys/capability.h&gt;</tt> (provided by <tt>libcap</tt>), and links against <tt>libcap</tt>. To obtain an environment in which the program can be compiled, run:
For example, suppose a Go program includes <code><tt>&lt;sys/capability.h&gt;</tt></code> (provided by <tt>libcap</tt>), and links against <tt>libcap</tt>. To obtain an environment in which the program can be compiled, run:


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
Line 109: Line 109:
If you intend to compile against glibc statically (such as via <tt>go build -ldflags "-s -w -linkmode external -extldflags -static"</tt>), add <tt>glibc.static</tt> to the list of packages passed to <tt>nix-shell</tt>.
If you intend to compile against glibc statically (such as via <tt>go build -ldflags "-s -w -linkmode external -extldflags -static"</tt>), add <tt>glibc.static</tt> to the list of packages passed to <tt>nix-shell</tt>.


If you encounter [https://github.com/go-delve/delve/issues/3085 this issue] and receive an error about _FORTIFY_SOURCE when running delve (for example in VSCode), put <tt>hardeningDisable = [ "fortify" ];</tt> inside shell.nix or in the <tt>mkShell</tt> invocation argument like this:
If you encounter [https://github.com/go-delve/delve/issues/3085 this issue] and receive an error about <code>_FORTIFY_SOURCE</code> when running delve (for example in [[Visual Studio Code|VSCode]]), put <tt>hardeningDisable = [ "fortify" ];</tt> inside <code>shell.nix</code> or in the <tt>mkShell</tt> invocation argument like this:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
pkgs.mkShell {
pkgs.mkShell {
Line 118: Line 118:


== Compile go program with static compile flag ==
== Compile go program with static compile flag ==
If <code>go build -ldflags "-s -w -linkmode external -extldflags -static"</code> fails on NixOS, with the error message <code>cannot find `-lpthread</code> and <code>cannot find -lc</code> - it is because the linker cannot find static glibc to link with. You need to have glibc.static in your environment (and have CFLAGS/LDFLAGS adjusted accordingly).
If <code>go build -ldflags "-s -w -linkmode external -extldflags -static"</code> fails on NixOS, with the error message <code>cannot find `-lpthread</code> and <code>cannot find -lc</code> - it is because the linker cannot find static glibc to link with. You need to have <tt>glibc.static</tt> in your environment (and have <code>CFLAGS</code>/<code>LDFLAGS</code> adjusted accordingly).
One way to achieve this is to have something like the following as <code>shell.nix</code> and run the compilation in a nix-shell:
One way to achieve this is to have something like the following as <code>shell.nix</code> and run the compilation in a <tt>nix-shell</tt>:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
Line 134: Line 134:
== Compile go program with static compile flag (take 2) ==
== Compile go program with static compile flag (take 2) ==


Linking against glibc.static does not really work because glibc does not really like static linking. You get a warning like <code>warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking</code>. To really create a static build, use musl. Example based on buildGoModule example from documentation:
Linking against <tt>glibc.static</tt> does not really work because glibc does not really like static linking. You get a warning like <code>warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking</code>. To really create a static build, use musl. Example based on <code>buildGoModule</code> example from documentation:


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>