LoginSignup
35
26

More than 5 years have passed since last update.

GO言語をMacで使ってみる インストール

Posted at

 環境

  • macOS
  • Homebrew

事前準備

Homebrewをインストールしておく

手順

インストール

$ brew install go
==> Downloading https://homebrew.bintray.com/bottles/go-1.9.4.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring go-1.9.4.high_sierra.bottle.tar.gz
==> Caveats
A valid GOPATH is required to use the `go get` command.
If $GOPATH is not specified, $HOME/go will be used by default:
  https://golang.org/doc/code.html#GOPATH

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> Summary
🍺  /usr/local/Cellar/go/1.9.4: 7,654 files, 294MB

バージョンの確認

$ go version
go version go1.9.4 darwin/amd64

Hello World!

hello.go
package main
import "fmt"

func main() {
    fmt.Printf("Hello World!\n")
}

実行

$ go run hello.go
Hello World!

ビルド

$ go build hello.go
$ ls
hello       hello.go
$ ./hello
Hello World!

覚書

癖で↓みたいに書いたら「syntax error」になった

hello.go
func main()
{
    fmt.Printf("Hello World!\n")
}

「syntax error」の件もあったのでgoのコーディング規約を検索

最初の方を少し読んだだけだけどインデントはハードタブらしい

gofmtコマンドで修正してくれるらしい

$ gofmt hello.go
package main

import "fmt"

func main() {
    fmt.Printf("Hello World!\n")
}

packageとimportの間に1行追加されてインデントがtabになりました

35
26
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
35
26