MacでGo言語のインストール【ハローワールドまで】

MacOSでGo言語をインストールしてハローワールドするまでの手順です。

Go言語のインストール手順

Go言語公式ページ(golang.org)にアクセスします。

「Download Go」をクリック。

「Apple macOS」をクリックしてインストーラーをダウンロードして起動します。

※過去のバージョンをインストールしたい場合は下の部分で選択します。

インストーラーを起動するとGo言語がインストールされます。

インストールの確認

ターミナルを新しく開き、バージョンとインストール先を確認します。

// バージョン確認
$ go version
go version go1.15.3 darwin/amd64

// インストール先の確認
$ which go
/usr/local/go/bin/go

ヘルプメッセージの確認

“go”とタイプしてヘルプメッセージが表示されます。

$ go
Go is a tool for managing Go source code.

Usage:

	go <command> [arguments]

The commands are:

	bug         start a bug report
	build       compile packages and dependencies
	clean       remove object files and cached files
	doc         show documentation for package or symbol
	env         print Go environment information
	fix         update packages to use new APIs
	fmt         gofmt (reformat) package sources
	generate    generate Go files by processing source
	get         add dependencies to current module and install them
	install     compile and install packages and dependencies
	list        list packages or modules
	mod         module maintenance
	run         compile and run Go program
	test        test packages
	tool        run specified go tool
	version     print Go version
	vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

	buildconstraint build constraints
	buildmode       build modes
	c               calling between Go and C
	cache           build and test caching
	environment     environment variables
	filetype        file types
	go.mod          the go.mod file
	gopath          GOPATH environment variable
	gopath-get      legacy GOPATH go get
	goproxy         module proxy protocol
	importpath      import path syntax
	modules         modules, module versions, and more
	module-get      module-aware go get
	module-auth     module authentication using go.sum
	module-private  module configuration for non-public modules
	packages        package lists and patterns
	testflag        testing flags
	testfunc        testing functions

Use "go help <topic>" for more information about that topic.

Hello Worldの実行

hello.goというファイルを作成し、実行します。

hello.go

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

実行します。go run [ファイル名] でコンパイルと実行をしてくれます。

$ go run hello.go
Hello, World!

Hello, Word!が表示されました。

コメントを残す

メールアドレスが公開されることはありません。