LoginSignup
0
0

More than 1 year has passed since last update.

Goのインストールとチュートリアルメモ

Last updated at Posted at 2021-08-02

Go使ったことない人間がGoのインストールしてから公式のチュートリアル進めて行くにあたって躓いた点などをメモ。
なおMac前提。(随時追記)

インストール

  • 公式から最新版をダウンロードしてインストールする
  • よくわからんけどgoコマンドが command not found になってしまうっ……!ってなったので、~/.bash_profileをいじいじしてパスを通す。
~/.bash_profile
# goのインストール先は/usr/local/goになる
export PATH="/usr/local/go/bin:$PATH"

そんでもってターミナル再起動かsourceで再読み込み

$ source ~/.bash_profile

これでようやくgoコマンドが動いた

$ go version
go version go1.16.6 darwin/amd64 

$GOPATH もなかったので指定しとく

~/.bash_profile
# GOPATHは適当に指定してもOK
export GOPATH=$HOME/.go

sourceで読み込んで準備完了。

$ source ~/.bash_profile
$ echo $GOPATH
/Users/xxxxx/.go

チュートリアル1: Get started with Go

前半について。
package main の意味とか、 go run 時の動作とかはこのサイトがすごくわかりやすかった。
go mod init (path)については、作成したmoduleをpublishする際には注意が必要ってことっぽい。

The name is the module's module path. In most cases, this will be the repository location where your source code will be kept, such as github.com/mymodule. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module.

後半について。
go mod tidyrsc.io/quote のモジュール本体がどこに行ったのかというと、 $GOPATH 配下にダウンロードされてた。なるほどね。

$GOPATH ── pkg ─┬─ mod   ─┬─ cache
                │         ├─ golang.org
                │         └─ rsc.io ── quote@v1.5.2 
                └─ sumdb 

go run . で結構時間かかるのはコンパイルしてるから。上に貼ったサイト参照。

※以降のチュートリアルは環境整っちゃえば大丈夫そう

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0