💨

Go言語の勉強のためにMacに環境を構築

2020/11/01に公開

Go言語を勉強しようと思い、Macに環境を構築してみた。

Install Go wth Homebrew

% brew install go
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
k3sup                                                        phive
==> Updated Formulae
Updated 114 formulae.

==> Downloading https://homebrew.bintray.com/bottles/go-1.15.3.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/6bd0119a31c1ae26831105490a67b97bed5d0fd2c6ad140cdf4c84aa7a315
######################################################################## 100.0%
==> Pouring go-1.15.3.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/go/1.15.3: 9,780 files, 494.3MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /usr/local/Cellar/fribidi/1.0.7... (66 files, 598.0KB)
Removing: /usr/local/Cellar/gettext/0.20.1... (1,893 files, 18.4MB)
Removing: /Users/username/Library/Caches/Homebrew/gh--1.0.0.catalina.bottle.tar.gz... (5.6MB)
Removing: /Users/username/Library/Caches/Homebrew/git--2.29.1.catalina.bottle.tar.gz... (14.6MB)
Removing: /usr/local/Cellar/gmp/6.1.2_2... (18 files, 3MB)
Removing: /usr/local/Cellar/gnutls/3.6.10... (1,229 files, 10MB)
Removing: /usr/local/Cellar/libevent/2.1.11_1... (1,063 files, 5MB)
Removing: /usr/local/Cellar/libffi/3.2.1... (16 files, 300.7KB)
Removing: /usr/local/Cellar/libidn2/2.2.0_1... (70 files, 709.3KB)
Removing: /usr/local/Cellar/libtasn1/4.14... (59 files, 399.9KB)
Removing: /usr/local/Cellar/nettle/3.4.1... (85 files, 2.1MB)
Removing: /usr/local/Cellar/openssl@1.1/1.1.1d... (7,983 files, 17.9MB)
Removing: /usr/local/Cellar/p11-kit/0.23.18.1... (63 files, 2.9MB)
Removing: /usr/local/Cellar/pcre2/10.33... (226 files, 5.8MB)
Removing: /usr/local/Cellar/pkg-config/0.29.2... (11 files, 623KB)
Removing: /usr/local/Cellar/readline/8.0.1... (48 files, 1.5MB)
Removing: /usr/local/Cellar/unbound/1.9.4... (57 files, 4.9MB)
Removing: /Users/username/Library/Logs/Homebrew/gh... (64B)
Pruned 0 symbolic links and 3 directories from /usr/local

Go version

% go version
go version go1.15.3 darwin/amd64

Go env

他の方の記事を読んだ感じだとGOPATHの設定は必要なくなったとか。

% go env  
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/username/Library/Caches/go-build"
GOENV="/Users/username/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/username/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/username/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.3/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/qv/x31rnmjx28s_hhv0wkbtpf1c0000gn/T/go-build468164307=/tmp/go-build -gno-record-gcc-switches -fno-common"

Try Tutorial

Go公式サイトの一番最初のチュートリアルを試す。

% mkdir hello && cd $_
% touch hello.go
hello.go
package main

import "fmt"

func main()  {
	fmt.Println("Hello, World!")
}
% go run hello.go 
Hello, World!

外部モジュールを利用する

  • ファイルを変更

"quote" packageを利用する。

hello.go
package main

import "fmt"

import "rsc.io/quote"

func main()  {
	// fmt.Println("Hello, World!")
	fmt.Println(quote.Go())
}
  • go.modファイルの作成
% go mod init hello
go: creating new go.mod: module hello
% cat go.mod 
module hello

go 1.15
  • Run code
% go run hello.go 
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
Don't communicate by sharing memory, share memory by communicating.

コードを実行する前にインポートしているパッケージ含んでいるモジュール(rsc.io/quote)をダウンロードする。

% cat go.mod 
module hello

go 1.15

require rsc.io/quote v1.5.2
% cat go.sum 
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

参考

Discussion