読者です 読者をやめる 読者になる 読者になる

mfks17's blog(Life is Good !!)

趣味や思った事を書いていくと思います

macにGo言語の環境を構築する

今回は、Go言語の開発環境を構築していきます。
興味は前々からあっていたのですが、放置してしまったので今回は環境だけでもやってしまいたいと思います。

環境

  • macbookair Late 2012
  • OS:Mountain Lion 10.8.2
  • Xcode4.5.2 & command line Toolsはインストール済み

インストール手順


1,環境変数の設定
参考:詳しい情報はここ−>http://golang.jp/install
## Go lang
export GOROOT=$HOME/go
export GOOS=darwin
export GOARCH=amd64
export PATH=$PATH:$GOROOT/bin

2,リポジトリからソースを引っ張ってくるのにmercurialを使います。
$ brew install mercurial
==> Downloading http://mercurial.selenic.com/release/mercurial-2.4.tar.gz
######################################################################## 100.0%
==> make local
==> Caveats
Extensions have been installed to:
/usr/local/Cellar/mercurial/2.4.2/libexec/hgext
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
/usr/local/Cellar/mercurial/2.4.2: 522 files, 6.9M, built in 9 seconds
$

3,ソースのクローン
$ hg clone -r release https://go.googlecode.com/hg/ $GOROOT
warning: go.googlecode.com certificate with fingerprint 0e:99:05:76:85:53:bb:69:f1:cf:99:b9:08:0e:73:bc:03:56:88:c7 not verified (check hostfingerprints or web.cacerts config setting)
adding changesets
adding manifests
adding file changes
added 13198 changesets with 49829 changes to 7139 files
updating to branch release-branch.go1
3213 files updated, 0 files merged, 0 files removed, 0 files unresolved

4,ソースのコンパイル

$ cd $GOROOT/src

$ ./all.bash
# Building C bootstrap tool.
cmd/dist

# Building compilers and Go bootstrap tool for host, darwin/amd64.
lib9
libbio
libmach
misc/pprof
cmd/addr2line
cmd/cov
cmd/nm
cmd/objdump
cmd/pack
cmd/prof
cmd/cc
cmd/gc
cmd/6l
cmd/6a
cmd/6c
cmd/6g
pkg/runtime
pkg/errors
pkg/sync/atomic
pkg/sync
pkg/io
pkg/unicode
pkg/unicode/utf8
pkg/unicode/utf16
pkg/bytes
pkg/math
pkg/strings
pkg/strconv
pkg/bufio
pkg/sort
pkg/container/heap
pkg/encoding/base64
pkg/syscall
pkg/time
pkg/os
pkg/reflect
pkg/fmt
pkg/encoding/json
pkg/flag
pkg/path/filepath
pkg/path
pkg/io/ioutil
pkg/log
pkg/regexp/syntax
pkg/regexp
pkg/go/token
pkg/go/scanner
pkg/go/ast
pkg/go/parser
pkg/os/exec
pkg/net/url
pkg/text/template/parse
pkg/text/template
pkg/go/doc
pkg/go/build
cmd/go

# Building packages and commands for darwin/amd64.
runtime
errors
sync/atomic
unicode
unicode/utf8
math
unicode/utf16
sync
crypto/subtle
container/list
io
syscall
hash
crypto
crypto/md5
hash/crc32
crypto/cipher
bytes
strings
path
bufio
time
text/tabwriter
crypto/hmac
crypto/sha1
html
hash/adler32
container/ring
crypto/sha256
crypto/sha512
hash/crc64
hash/fnv
image/color
os
strconv
sort
math/rand
math/cmplx
path/filepath
container/heap
compress/bzip2
os/signal
reflect
regexp/syntax
io/ioutil
net/url
os/exec
regexp
encoding/base64
crypto/aes
crypto/rc4
encoding/pem
encoding/ascii85
encoding/base32
image
fmt
encoding/binary
image/draw
image/jpeg
debug/dwarf
crypto/des
index/suffixarray
flag
go/token
text/template/parse
go/scanner
log
debug/elf
go/ast
text/template
debug/macho
debug/pe
encoding/json
go/doc
go/parser
go/printer
go/build
encoding/xml
compress/flate
math/big
cmd/api
cmd/cgo
cmd/fix
compress/gzip
crypto/elliptic
crypto/rand
crypto/rsa
crypto/dsa
encoding/asn1
mime
runtime/debug
archive/zip
crypto/x509/pkix
encoding/gob
html/template
runtime/cgo
runtime/pprof
text/scanner
cmd/gofmt
cmd/vet
cmd/yacc
crypto/x509
net
archive/tar
compress/lzw
compress/zlib
crypto/ecdsa
database/sql/driver
debug/gosym
database/sql
encoding/csv
encoding/hex
image/gif
image/png
os/user
testing
testing/iotest
testing/quick
crypto/tls
net/textproto
log/syslog
mime/multipart
net/mail
net/http
net/smtp
cmd/go
expvar
net/http/pprof
net/http/cgi
net/http/httptest
cmd/godoc
net/http/fcgi
net/http/httputil
net/rpc
net/rpc/jsonrpc

# Testing packages.
ok cmd/api 0.021s
? cmd/cgo [no test files]
ok cmd/fix 1.963s
ok cmd/go 0.037s
? cmd/godoc [no test files]
ok cmd/gofmt 0.061s
? cmd/vet [no test files]
? cmd/yacc [no test files]
ok archive/tar 0.012s
ok archive/zip 0.058s
ok bufio 0.061s
ok bytes 0.042s
ok compress/bzip2 0.074s
ok compress/flate 0.168s
ok compress/gzip 0.018s
ok compress/lzw 0.027s
ok compress/zlib 0.163s
ok container/heap 0.012s
ok container/list 0.014s
ok container/ring 0.022s
? crypto [no test files]
ok crypto/aes 0.043s
ok crypto/cipher 0.012s
ok crypto/des 0.037s
ok crypto/dsa 0.017s
ok crypto/ecdsa 0.054s
ok crypto/elliptic 0.018s
ok crypto/hmac 0.011s
ok crypto/md5 0.013s
ok crypto/rand 0.039s
ok crypto/rc4 0.016s
ok crypto/rsa 0.127s
ok crypto/sha1 0.013s
ok crypto/sha256 0.013s
ok crypto/sha512 0.017s
ok crypto/subtle 0.021s
ok crypto/tls 0.128s
ok crypto/x509 0.442s
? crypto/x509/pkix [no test files]
ok database/sql 0.019s
ok database/sql/driver 0.012s
ok debug/dwarf 0.017s
ok debug/elf 0.020s
ok debug/gosym 0.014s
ok debug/macho 0.014s
ok debug/pe 0.012s
ok encoding/ascii85 0.012s
ok encoding/asn1 0.013s
ok encoding/base32 0.011s
ok encoding/base64 0.011s
ok encoding/binary 0.011s
ok encoding/csv 0.012s
ok encoding/gob 0.053s
ok encoding/hex 0.011s
ok encoding/json 0.072s
ok encoding/pem 0.014s
ok encoding/xml 0.025s
ok errors 0.012s
ok expvar 0.021s
ok flag 0.010s
ok fmt 0.059s
ok go/ast 0.014s
ok go/build 0.171s
ok go/doc 0.090s
ok go/parser 0.076s
ok go/printer 0.393s
ok go/scanner 0.012s
ok go/token 0.057s
? hash [no test files]
ok hash/adler32 0.011s
ok hash/crc32 0.010s
ok hash/crc64 0.010s
ok hash/fnv 0.012s
ok html 0.015s
ok html/template 0.071s
ok image 0.120s
ok image/color 0.019s
ok image/draw 0.037s
? image/gif [no test files]
ok image/jpeg 0.090s
ok image/png 0.062s
ok index/suffixarray 0.016s
ok io 0.025s
ok io/ioutil 0.011s
ok log 0.011s
ok log/syslog 0.419s
ok math 0.013s
ok math/big 0.129s
ok math/cmplx 0.012s
ok math/rand 0.189s
ok mime 0.013s
ok mime/multipart 0.219s
ok net 0.892s
ok net/http 3.179s
ok net/http/cgi 0.705s
ok net/http/fcgi 0.023s
ok net/http/httptest 0.022s
ok net/http/httputil 0.059s
? net/http/pprof [no test files]
ok net/mail 0.015s
ok net/rpc 0.107s
ok net/rpc/jsonrpc 0.026s
ok net/smtp 0.023s
ok net/textproto 0.017s
ok net/url 0.010s
ok os 0.049s
ok os/exec 0.504s
ok os/signal 0.012s
ok os/user 0.025s
ok path 0.013s
ok path/filepath 0.194s
ok reflect 0.014s
ok regexp 0.120s
ok regexp/syntax 1.011s
ok runtime 0.405s
? runtime/cgo [no test files]
ok runtime/debug 0.010s
ok runtime/pprof 0.472s
ok sort 0.096s
ok strconv 0.547s
ok strings 0.013s
ok sync 0.077s
ok sync/atomic 0.035s
? syscall [no test files]
? testing [no test files]
? testing/iotest [no test files]
ok testing/quick 0.037s
ok text/scanner 0.020s
ok text/tabwriter 0.015s
ok text/template 0.038s
ok text/template/parse 0.016s
ok time 2.271s
ok unicode 0.011s
ok unicode/utf16 0.008s
ok unicode/utf8 0.013s
? unsafe [no test files]

real 0m29.821s
user 1m18.094s
sys 0m11.776s

# GOMAXPROCS=2 runtime -cpu=1,2,4
ok runtime 0.728s

# sync -cpu=10
ok sync 0.220s

# ../misc/cgo/stdio

# ../misc/cgo/life

# ../misc/cgo/test
scatter = 0x2936e
hello from C
PASS
ok _/Users/typosterr/go/misc/cgo/test 1.098s

# ../doc/progs

real 0m7.961s
user 0m6.371s
sys 0m1.301s

# ../doc/articles/wiki
rm -f srcextract.bin htmlify.bin get.bin
PASS

# ../doc/codewalk

# ../misc/dashboard/builder ../misc/goplay

# ../test/bench/shootout
fasta
reverse-complement
nbody
binary-tree
binary-tree-freelist
fannkuch
fannkuch-parallel
regex-dna
regex-dna-parallel
spectral-norm
k-nucleotide
k-nucleotide-parallel
mandelbrot
meteor-contest
pidigits
threadring
chameneosredux

# ../test/bench/go1
ok _/Users/typosterr/go/test/bench/go1 2.722s

# ../test

real 0m12.381s
user 0m33.139s
sys 0m9.286s

# Checking API compatibility.
Go version is "go1.0.3", ignoring -next /Users/typosterr/go/api/next.txt

ALL TESTS PASSED

    • -

Installed Go for darwin/amd64 in /Users/typosterr/go
Installed commands in /Users/typosterr/go/bin


5,Hello World

ではいつものあれをやってみましょう。

# 適当なディレクトリに移動
$ cd ~/src
$ cat >hello.go < > package main
>
> import "fmt"
>
> func main() {
> fmt.Printf("hello world\n")
> }
> EOF
$ go run hello.go
hello world
$

後書き


前にやってみたときは、CPUのアーキテクチャによってビルドするコマンドが異なっていたと思うのですが(6gとか8gとか)現在はgoツールというのに統合されてました。簡単ですね。
最近のmacとかだとコンパイラGCCからApple llvmになって、ちょっとややこしいものとかがあってりしますが、特に問題はなかったです。

まとめ


Go言語の開発環境を構築しました。
特につまずいた感じはなかったです。
マスコットが相変わらずユルいですね。

Featured video