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

Go で Package を Build

  • うまくいかなくなったビルドが元通りにうまくいくようになったので、とりあえずそのメモ
$ env | grep GO
GOROOT=/usr/local/Cellar/go/1.5.1/libexec
GOPATH=/Users/nishidy/.go

$ head -n1 *go
==> count.go <==
package ParseWikipediaXML

==> parse.go <==
package ParseWikipediaXML

==> redis_client.go <==
package ParseWikipediaXML

==> tfidf.go <==
package ParseWikipediaXML

$ cp *.go $GOROOT/src/ParseWikipediaXML/

$ ls $GOROOT/src/ParseWikipediaXML/
count.go  parse.go  redis_client.go  tfidf.go

$ go build -x ParseWikipediaXML
WORK=/var/folders/py/ckd84qtn5gz415br68fzzz6w0000gn/T/go-build372571907
mkdir -p $WORK/ParseWikipediaXML/_obj/
mkdir -p $WORK/
cd /usr/local/Cellar/go/1.5.1/libexec/src/ParseWikipediaXML
/usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64/compile -o $WORK/ParseWikipediaXML.a -trimpath $WORK -p ParseWikipediaXML -complete -buildid fbfb2249b3c733faf3613e823f1ae8ca07ba6fb3 -D _/usr/local/Cellar/go/1.5.1/libexec/src/ParseWikipediaXML -I $WORK -I /Users/nishidy/.go/pkg/darwin_amd64 -pack ./count.go ./parse.go ./redis_client.go ./tfidf.go

$ go install -x ParseWikipediaXML
WORK=/var/folders/py/ckd84qtn5gz415br68fzzz6w0000gn/T/go-build655772754
mkdir -p $WORK/ParseWikipediaXML/_obj/
mkdir -p $WORK/
cd /usr/local/Cellar/go/1.5.1/libexec/src/ParseWikipediaXML
/usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64/compile -o $WORK/ParseWikipediaXML.a -trimpath $WORK -p ParseWikipediaXML -complete -buildid fbfb2249b3c733faf3613e823f1ae8ca07ba6fb3 -D _/usr/local/Cellar/go/1.5.1/libexec/src/ParseWikipediaXML -I $WORK -I /Users/nishidy/.go/pkg/darwin_amd64 -pack ./count.go ./parse.go ./redis_client.go ./tfidf.go
mkdir -p /usr/local/Cellar/go/1.5.1/libexec/pkg/darwin_amd64/
mv $WORK/ParseWikipediaXML.a /usr/local/Cellar/go/1.5.1/libexec/pkg/darwin_amd64/ParseWikipediaXML.a

# すると以下にスタティックライブラリが作成される

$ ls $GOROOT/pkg/darwin_amd64/ | grep ParseWikipediaXML
ParseWikipediaXML.a

# あとはこれをimportすれば良い

$ cat main.go
package main

import (
   pxml "ParseWikipediaXML"
...
)
...

$ go build -x main.go
WORK=/var/folders/py/ckd84qtn5gz415br68fzzz6w0000gn/T/go-build988424706
mkdir -p $WORK/command-line-arguments/_obj/
mkdir -p $WORK/command-line-arguments/_obj/exe/
cd /Users/nishidy/Documents/git/ParseWikipediaXML/go/main
/usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64/compile -o $WORK/command-line-arguments.a -trimpath $WORK -p main -complete -buildid 6d5db34fe7bb1b4b80c5e874d09cbbd020df25ab -D _/Users/nishidy/Documents/git/ParseWikipediaXML/go/main -I $WORK -pack ./main.go
cd .
/usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64/link -o $WORK/command-line-arguments/_obj/exe/a.out -L $WORK -L /Users/nishidy/.go/pkg/darwin_amd64 -extld=clang -buildmode=exe -buildid=6d5db34fe7bb1b4b80c5e874d09cbbd020df25ab $WORK/command-line-arguments.a
mv $WORK/command-line-arguments/_obj/exe/a.out main

# ファイルを更新した場合は、作成したライブラリを消して再ビルド

$ rm GOROOT/pkg/darwin_amd64/ParseWikipediaXML.a
$ cp *.go $GOROOT/src/ParseWikipediaXML/
$ go build ParseWikipediaXML
$ go install ParseWikipediaXML
$ go build main.go

# ライブラリを消すのにgo cleanは使えない

$ go clean -x ParseWikipediaXML
cd /usr/local/Cellar/go/1.5.1/libexec/src/ParseWikipediaXML
rm -f ParseWikipediaXML.test ParseWikipediaXML.test.exe
$
# go get したものは$GOPATH以下にインストールされる

$ ls $GOPATH
bin  pkg  src

$ cat main.go
package main

import (
   pxml "github.com/nishidy/ParseWikipediaXML"
...
)
...

# すると$GOPATHがsearch pathに追加される

$ go build -x main.go 
WORK=/var/folders/py/ckd84qtn5gz415br68fzzz6w0000gn/T/go-build264272204
mkdir -p $WORK/command-line-arguments/_obj/
mkdir -p $WORK/command-line-arguments/_obj/exe/
cd /Users/nishidy/Documents/git/ParseWikipediaXML/go/main
/usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64/compile -o $WORK/command-line-arguments.a -trimpath $WORK -p main -complete -buildid b461d8c26a7bd15d8303385abe3e39199b894a08 -D _/Users/nishidy/Documents/git/ParseWikipediaXML/go/main -I $WORK -I /Users/nishidy/.go/pkg/darwin_amd64 -pack ./main.go
cd .
/usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64/link -o $WORK/command-line-arguments/_obj/exe/a.out -L $WORK -L /Users/nishidy/.go/pkg/darwin_amd64 -extld=clang -buildmode=exe -buildid=b461d8c26a7bd15d8303385abe3e39199b894a08 $WORK/command-line-arguments.a
mv $WORK/command-line-arguments/_obj/exe/a.out main

$ /usr/local/Cellar/go/1.5.1/libexec/pkg/tool/darwin_amd64/compile --help
...
  -I directory
        add directory to import search path
...