Go言語を触るための環境構築 ~Linux編~

とりあえず、goが触れる環境をつくりましょう。

Go言語を触るための環境構築 ~Linux編~
目次

1. 環境

  • Docker
  • Ubuntu
  • Go

2. 公式パッケージの取得

公式ドキュメント

wget https://go.dev/dl/go1.18.3.linux-amd64.tar.gz

3. 展開して配置

sudo tar -C /usr/local -xzf go1.18.3.linux-amd64.tar.gz

4. パスを通す

export PATH=$PATH:/usr/local/go/bin

5. シェル再起動

exec $SHELL -l

6. コマンド確認

go version

7. godoc

インストール

$ go install golang.org/x/tools/cmd/godoc@latest
go: downloading golang.org/x/sys v0.0.0-20211019181941-9d821ace8654
go: downloading golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f
$ godoc
using module mode; GOMOD=/dev/null

PATHを通す

$ export GOPATH=$(go env GOPATH)
$ export PATH=$PATH:$GOPATH/bin

GOROOT/binをPATH環境変数に追加しているのは、godocのコマンドのインストール先がここになるから。(通常は、GOPATH/binにインストールされるらしい)

コマンドラインで表示

$ go doc fmt Println
package fmt // import "fmt"

func Println(a ...any) (n int, err error)
    Println formats using the default formats for its operands and writes to
    standard output. Spaces are always added between operands and a newline is
    appended. It returns the number of bytes written and any write error
    encountered.

ブラウザで表示

godoc -http ":5000"