tags: - golang - setup categories: - informational comments: true
Install the Go compiler and write a few small programs to get familiar with the language. We will be using Go to implement parts of our network automation infrastructure.
The build output should be byte for byte reproducible from the inputs so we can recreate binaries and trace their provenance.
sudo apt install –no-install-recommends git curl
git clone https://github.com/asdf-vm/asdf.git ~/.asdf
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.profile
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.profile
. ~/.profile
asdf
asdf plugin add golang
asdf list-all golang
asdf install golang 1.17.4
asdf global golang 1.17.4
asdf local golang 1.17.4
asdf plugin add golangci-lint
asdf list-all golangci-lint
asdf install golangci-lint 1.43.0
mkdir test
cd test
go mod init test
In an editor, create main.go:
vi main.go
With contents:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Fprintln(os.Stderr, "test")
}
Initialize a git repository
git init
# add .gitignore from: https://github.com/github/gitignore/blob/master/Go.gitignore
git add .
# sign your commits
git commit -S
Ensure the files are properly formatted:
go fmt main.go
git diff
Compile the program
go build
Run the program
./test
Compile the program as a reproducible build: investigate the differences between the binaries
CGO_ENABLED
: disable use of libc, use pure go
-trimpath: remove paths from stacktraces
-ldflags: reduce binary size by removing debug tables
CGO_ENABLED=0 go build -trimpath -ldflags "-s -w"
Run the linters
golangci-lint run
golangci-lint run --enable-all
Write the following programs. We can review during the meeting.
Create a program which lists environment variables as ‘=’ delimited key/value pairs:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=e6a0557d8a1b
Create a program which lists information about network interfaces:
* interface name
* MAC address
* IP addresses
{Index:1 MTU:65536 Name:lo HardwareAddr: Flags:up|loopback}
[127.0.0.1/32]