Debian系LinuxにGo言語1.17.2をインストール
まずはダウンロード
Downloads - The Go Programming Language
ここからLinux版をダウンロード。
自分のパソコンのCPUアーキテクチャに合わせて選ばないとダメなので注意です。
私の場合は、Lenovo X260モデル(64bit)なので「go1.17.2.linux-amd64.tar.gz」になります。
インテル系32bitCPUの場合は「go1.17.2.linux-386.tar.gz」かな。
RaspberryPi4の場合は「go1.17.2.linux-armv6l.tar.gz」ですね。
インストール
Download and install - The Go Programming Language
こちらのサイトに、Linuxインストール方法が記載されています。
tar.gzファイルを任意の場所に解凍
本家からダウンロードしたtar.gzファイルを、任意のディレクトリで解凍します。
※ファイラーでもコマンドラインでも・・・その方法はGoogle先生に質問で・・w
私は、ホームディレクトリは以下のoptディレクトリ内に配置しました。
$ cd ~/opt/golang/1.17.2/go $ tree -d -L 1 . ├── api ├── bin ├── doc ├── lib ├── misc ├── pkg ├── src └── test
mainモジュール作成
Go言語のインストールが完了したら、いよいよソースコード書いてコンパイルしてみます。
任意のディレクトリを作成し、そこをソースコード置き場としていきます。
Goのバージョン1.16以前は、環境変数GOPATH配下に配置したディレクトリにまとめて置いていた様子ですが、1.17からは任意のディレクトリにソースを配置できます。
本記事では、「~/opt/golang/1.17.2/go/src」に置くことにしましょう。
ディレクトリ作成
$ cd ~/opt/golang/1.17.2/go $ mkdir ./hello
モジュールコマンドによる参照モジュールの指定
このコマンドにより、以前あったGOPATHに縛られないソースコード配置が可能となります。
$ cd ~/opt/golang/1.17.2/go/hello $ mkdir ./hello
$ go mod init example.com/hello
ソースコード作成
$ cd ~/opt/golang/1.17.2/go/hello $ nano ./hello.go
package hello import "fmt" func main() { fmt.Println("Hello, World!") }
実行
$ ./hello.exe Hello, World!
補足
環境変数GOPATHの設定は?
バージョン1.17から、GOPATHによるソースコード(パッケージ)の依存関係管理は非推奨になった模様。
それにともない、パッケージのインストール方法も「go get」ではなく「go install」が良いよと警告文が表示されるようになった。
$ go get github.com/future-architect/awsmfa/cmd/awsmfa go: downloading github.com/future-architect/awsmfa v1.1.1 go: downloading gopkg.in/ini.v1 v1.62.0 go get: installing executables with 'go get' in module mode is deprecated. Use 'go install pkg@version' instead. For more information, see https://golang.org/doc/go-get-install-deprecation or run 'go help get' or 'go help install'.
よって、本記事では環境変数GOPATHはインストール初期状態のままとし、任意のフォルダにソースを配置して管理する方法を紹介しています。
mainモジュールへ独自モジュールを読み込む時は?
例えば、下記のように配置したとします。
$ cd ~/opt/golang/1.17.2/go/src $ tree -d -L 1 . ├── bar <- 自作モジュールbar ├── foo <- 自作モジュールfoo ├── hello <- メインモジュール
それぞれのディレクトリで、下記の通りgo mod コマンドを実行します。
$ cd ~/opt/golang/1.17.2/go/src/bar $ go mod init example.com/bar $ cd ~/opt/golang/1.17.2/go/src/foo $ go mod init example.com/foo $ cd ~/opt/golang/1.17.2/go/src/hello $ go mod init example.com/hello
その後、helloディレクトリで下記の追加コマンドを実行します
このコマンド実行により、URLによる外部モジュール参照ではなく、ローカルディレクトリのモジュール参照に置き換わります。
参照元がhelloディレクトリのhello.goから見た相対パスになるので、それぞれ「../bar」と「../foo」になります。
$ cd ~/opt/golang/1.17.2/go/src/hello $ go mod edit -replace example.com/bar=../bar $ go mod edit -replace example.com/bar=../foo $ go mod tidy
その後、helloディレクトリにてプログラム実行
$ go run .
以下、それぞれのソースコードです。
■~/opt/golang/1.17.2/go/src/bar/bar.go
package bar import "fmt" // Hello returns a greeting for the named person. func Hello(name string) string { // Return a greeting that embeds the name in a message. message := fmt.Sprintf("Hi, %v. Welcome!", name) return message }
■~/opt/golang/1.17.2/go/src/foo/foo.go
package foo import "fmt" // Hello returns a greeting for the named person. func Hello(name string) string { // Return a greeting that embeds the name in a message. message := fmt.Sprintf("Yahoo, %v. FooBar!", name) return message }
■~/opt/golang/1.17.2/go/src/hello/hello.go
package hello import ( "fmt" "example.com/bar" "example.com/foo" ) func main() { // Get a greeting message and print it. message := bar.Hello("hoge") message2 := foo.Hello("fuga") fmt.Println(message) fmt.Println(message2) }
オススメパッケージのインストール
$ go install golang.org/x/tools/gopls@latest $ go install golang.org/x/tools/cmd/godoc@latest $ go install github.com/go-delve/delve/cmd/dlv@latest $ go install github.com/gin-gonic/gin@latest $ go install github.com/rogpeppe/godef@latest $ go install github.com/mum4k/termdash@latest $ go install github.com/jstemmer/gotags@latest