Go: Difference between revisions
DerpishCat (talk | contribs) m (Fixed whitespace and expanded contractions) |
(Remove go-bindata - it's redundant with the embed package in the standard library, only used as pkg dep) |
||
(44 intermediate revisions by 7 users not shown) | |||
Line 7: | Line 7: | ||
{{Related|Go package guidelines}} | {{Related|Go package guidelines}} | ||
{{Related articles end}} | {{Related articles end}} | ||
[https://go.dev/ Go] is an open source programming language supported by Google. From [https://go.dev/doc/ the Go documentation]: | |||
[https:// | |||
:Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language. | |||
== Installation == | == Installation == | ||
{{ | [[Install]] the {{pkg|go}} package, which includes the standard Go compiler and other development tools. See the [https://pkg.go.dev/cmd/go ''go'' command documentation] for a list of included subcommands. | ||
=== Alternative Compilers === | |||
The {{pkg|go}} package includes the standard Go compiler, which is called ''gc''. The following alternative compilers are also available. | |||
==== gccgo ==== | |||
A frontend for [[GCC]] called [https://go.dev/doc/install/gccgo gccgo] is provided by the {{pkg|gcc-go}} package. ''gccgo'' may produce faster binaries than ''gc'' in some cases and can target additional operating systems and architectures. [https://meltware.com/2019/01/16/gccgo-benchmarks-2019.html In practice, ''gc'' produces faster binaries than ''gccgo'' for almost all workloads.] | |||
==== TinyGo ==== | |||
[https://tinygo.org/ TinyGo] is an [[LLVM]] based compiler designed to produce very small binaries for embedded systems and WebAssembly. It is provided by the {{pkg|tinygo}} package. | |||
=== Tools === | |||
The following packages provide developer tools for Go: | |||
You can | * {{App|Go tools|Various tools and Go packages mostly for static analysis of Go programs.|https://cs.opensource.google/go/x/tools|{{Pkg|go-tools}}}} | ||
* {{App|gopls|The official Go language server.|https://pkg.go.dev/golang.org/x/tools/gopls|{{Pkg|gopls}}}} | |||
* {{App|Delve|A debugger for the Go programming language.|https://github.com/go-delve/delve|{{Pkg|delve}}}} | |||
* {{App|GoReleaser|A release automation tool for Go projects.|https://goreleaser.com/|{{Pkg|goreleaser}}}} | |||
* {{App|gox|A tool for Go cross compilation that will parallelize builds for multiple platforms.|https://github.com/mitchellh/gox|{{Pkg|gox}}}} | |||
* {{App|ko|A container image builder for Go applications.|https://github.com/ko-build/ko|{{Pkg|ko}}}} | |||
* {{App|revive|A fast, configurable, extensible, flexible, and beautiful linter for Go.|https://revive.run/|{{Pkg|revive}}}} | |||
* {{App|Staticcheck|A state of the art linter for the Go programming language.|https://staticcheck.io/|{{Pkg|staticcheck}}}} | |||
* {{App|Yaegi|A Go interpreter. Includes the ''yaegi'' command-line interpreter/REPL.|https://github.com/traefik/yaegi|{{Pkg|yaegi}}}} | |||
=== Install directory === | |||
The {{ic|go install}} command installs Go executables in the directory named by the {{ic|GOBIN}} [[environment variable]]. {{ic|GOBIN}} defaults to {{ic|$GOPATH/bin}}, or {{ic|~/go/bin}} if the {{ic|GOPATH}} environment variable is not set. | |||
{{Tip|You can see all Go variables by running {{ic|go env}}}} | |||
For convenience, add the bin subdirectory to {{ic|PATH}}: | |||
$ export PATH="$PATH:$(go env GOBIN):$(go env GOPATH)/bin" | |||
See [https://go.dev/doc/code How to Write Go Code] and {{ic|go help install}} for more information. | |||
== Tips and Tricks == | |||
=== Compiling source code === | |||
You can write a Hello World program as follows: | |||
{{hc|hello.go| | {{hc|hello.go| | ||
Line 33: | Line 69: | ||
}} | }} | ||
Then run it with the go tool: | Then run it with the ''go'' tool: | ||
{{hc|$ go run hello.go| | {{hc|$ go run hello.go| | ||
Hello, Arch! | Hello, Arch! | ||
}} | }} | ||
Compilation with standard | Compilation with the standard compiler (same as {{ic|1=go build -compiler=gc hello.go}}): | ||
$ go build hello.go | $ go build hello.go | ||
Line 46: | Line 83: | ||
$ gccgo hello.go -o hello | $ gccgo hello.go -o hello | ||
Compilation with ''tinygo'': | |||
$ tinygo build -o hello ./hello.go | |||
=== Cross compiling to other platforms === | |||
The standard compiler can natively cross-compile to [https://go.dev/doc/install/source#introduction a number of platforms]. The procedure varies depending on whether the source code calls C code using [https://pkg.go.dev/cmd/cgo cgo]. | |||
==== Without cgo ==== | |||
If cgo is not required for your build, then simply specify the target OS and architecture as [[environment variables]] to {{ic|go build}}: | |||
$ GOOS=linux GOARCH=arm64 go build . | |||
See [https://go.dev/doc/install/source#environment the official documentation] for the valid combinations of {{ic|GOOS}} and {{ic|GOARCH}}. | |||
==== With cgo ==== | |||
If cgo is required for your build, you have to provide the path to your C/C++ cross-compilers, via the {{ic|CC}}/{{ic|CXX}} environment variables. | |||
Say you want to cross-compile for {{ic|1=GOOS=linux}} and {{ic|1=GOARCH=arm64}}. | |||
You need first to install the {{Pkg|aarch64-linux-gnu-gcc}} cross-compiler. | |||
Here is a sample program that requires cgo, so that we can test the cross-compilation process: | |||
{{hc|hello.go| | |||
package main | |||
// #include <stdio.h> | |||
// void hello() { puts("Hello, Arch!"); } | |||
import "C" | |||
func main() { | |||
C.hello() | |||
} | |||
}} | |||
Then, you can cross-compile it like this: | Then, you can cross-compile it like this: | ||
Line 96: | Line 125: | ||
$ GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC=/usr/bin/aarch64-linux-gnu-gcc go build hello.go | $ GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC=/usr/bin/aarch64-linux-gnu-gcc go build hello.go | ||
You can check that the architecture of the generated binary is actually | You can check that the architecture of the generated binary is actually aarch64: | ||
$ file hello | $ file hello | ||
Line 107: | Line 136: | ||
[alarm@arpi3 ~]$ ./hello | [alarm@arpi3 ~]$ ./hello | ||
Hello, Arch! | Hello, Arch! | ||
=== Using an alternate Go module mirror === | |||
By default, Go uses Google's service [https://proxy.golang.org/ proxy.golang.org] as a module mirror. | |||
If an alternate mirror is desired, it can be changed with the [[environment variable]] {{ic|GOPROXY}}, for example: | |||
$ export GOPROXY=https://goproxy.io/ | |||
A number of public module mirrors are available, see for example: [https://hugomods.com/en/blog/2023/04/go-and-hugo-proxy-servers/ Go and Hugo Proxy Servers]. | |||
== Troubleshooting == | == Troubleshooting == | ||
=== | === JetBrains Go Plugin === | ||
If you are using a | If you are using a JetBrains IDE and the Go plugin cannot find your Go SDK path, you might be using an incompatible package. Remove the ''gcc-go'' package and replace it with ''go''. If your {{ic|GOPATH}} is set, the IDE should now be able to find your Go SDK at {{ic|/usr/lib/go}}. | ||
== See also == | == See also == | ||
* [https:// | * [https://go.dev/ Official website] | ||
* [[Wikipedia:Go (programming language)|Wikipedia article]] | * [[Wikipedia:Go (programming language)|Wikipedia article]] | ||
* [https://gobyexample.com/ Examples with small descriptions] | * [https://gobyexample.com/ Examples with small descriptions] | ||
* [https://tour | * [https://go.dev/tour Interactive Go training tour] | ||
* [https://rakyll.org/cross-compilation/ Go cross compilation] | * [https://rakyll.org/cross-compilation/ Go cross compilation] | ||
* [https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins IDEs and Plugins for Go] | * [https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins IDEs and Plugins for Go] |
Latest revision as of 03:17, 2 April 2024
Go is an open source programming language supported by Google. From the Go documentation:
- Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.
Installation
Install the go package, which includes the standard Go compiler and other development tools. See the go command documentation for a list of included subcommands.
Alternative Compilers
The go package includes the standard Go compiler, which is called gc. The following alternative compilers are also available.
gccgo
A frontend for GCC called gccgo is provided by the gcc-go package. gccgo may produce faster binaries than gc in some cases and can target additional operating systems and architectures. In practice, gc produces faster binaries than gccgo for almost all workloads.
TinyGo
TinyGo is an LLVM based compiler designed to produce very small binaries for embedded systems and WebAssembly. It is provided by the tinygo package.
Tools
The following packages provide developer tools for Go:
- Go tools — Various tools and Go packages mostly for static analysis of Go programs.
- gopls — The official Go language server.
- Delve — A debugger for the Go programming language.
- GoReleaser — A release automation tool for Go projects.
- gox — A tool for Go cross compilation that will parallelize builds for multiple platforms.
- ko — A container image builder for Go applications.
- revive — A fast, configurable, extensible, flexible, and beautiful linter for Go.
- Staticcheck — A state of the art linter for the Go programming language.
- Yaegi — A Go interpreter. Includes the yaegi command-line interpreter/REPL.
Install directory
The go install
command installs Go executables in the directory named by the GOBIN
environment variable. GOBIN
defaults to $GOPATH/bin
, or ~/go/bin
if the GOPATH
environment variable is not set.
go env
For convenience, add the bin subdirectory to PATH
:
$ export PATH="$PATH:$(go env GOBIN):$(go env GOPATH)/bin"
See How to Write Go Code and go help install
for more information.
Tips and Tricks
Compiling source code
You can write a Hello World program as follows:
hello.go
package main import "fmt" func main() { fmt.Println("Hello, Arch!") }
Then run it with the go tool:
$ go run hello.go
Hello, Arch!
Compilation with the standard compiler (same as go build -compiler=gc hello.go
):
$ go build hello.go
Compilation with gccgo (same as go build -compiler=gccgo hello.go
):
$ gccgo hello.go -o hello
Compilation with tinygo:
$ tinygo build -o hello ./hello.go
Cross compiling to other platforms
The standard compiler can natively cross-compile to a number of platforms. The procedure varies depending on whether the source code calls C code using cgo.
Without cgo
If cgo is not required for your build, then simply specify the target OS and architecture as environment variables to go build
:
$ GOOS=linux GOARCH=arm64 go build .
See the official documentation for the valid combinations of GOOS
and GOARCH
.
With cgo
If cgo is required for your build, you have to provide the path to your C/C++ cross-compilers, via the CC
/CXX
environment variables.
Say you want to cross-compile for GOOS=linux
and GOARCH=arm64
.
You need first to install the aarch64-linux-gnu-gcc cross-compiler.
Here is a sample program that requires cgo, so that we can test the cross-compilation process:
hello.go
package main // #include <stdio.h> // void hello() { puts("Hello, Arch!"); } import "C" func main() { C.hello() }
Then, you can cross-compile it like this:
$ GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC=/usr/bin/aarch64-linux-gnu-gcc go build hello.go
You can check that the architecture of the generated binary is actually aarch64:
$ file hello hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=b1d92ae8840a019f36cc2aee4606b6ae4a581bf1, for GNU/Linux 3.7.0, not stripped
If you copy hello
to a suitable host, you can test-run it:
[alarm@rpi3 ~]$ uname -a Linux alarm 5.3.8-1-ARCH #1 SMP Tue Oct 29 19:31:23 MDT 2019 aarch64 GNU/Linux [alarm@arpi3 ~]$ ./hello Hello, Arch!
Using an alternate Go module mirror
By default, Go uses Google's service proxy.golang.org as a module mirror.
If an alternate mirror is desired, it can be changed with the environment variable GOPROXY
, for example:
$ export GOPROXY=https://goproxy.io/
A number of public module mirrors are available, see for example: Go and Hugo Proxy Servers.
Troubleshooting
JetBrains Go Plugin
If you are using a JetBrains IDE and the Go plugin cannot find your Go SDK path, you might be using an incompatible package. Remove the gcc-go package and replace it with go. If your GOPATH
is set, the IDE should now be able to find your Go SDK at /usr/lib/go
.