Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Goの標準的な開発の流れ

 Goの標準的な開発の流れ

スライド「Go一緒にいかが?」の資料1

Ryuji Iwata

December 23, 2014
Tweet

More Decks by Ryuji Iwata

Other Decks in Programming

Transcript

  1. Goの標準的な開発の流れ オリジナル動画 Writing, building, installing, and testing Go code https://www.youtube.com/watch?v=XCsL89YtqCs

    関連サイト How to Write Go Code http://golang.org/doc/code.html 以下、コマンド類につきましては「Mac OS X」を使用しております。 事前に知っておこう! 環境変数の確認 ~$ echo $HOME /Users/ユーザー名 ~$ go env GOOS darwin ~$ go env GOARCH amd64 GOOSとGOARCHの値と組み合わせ Installing Go from source - Optional environment variables http://golang.org/doc/install/source#environment $GOOS $GOARCH -------- -------- darwin 386 darwin amd64 freebsd 386 freebsd amd64 freebsd arm linux 386 linux amd64 linux arm netbsd 386 netbsd amd64 netbsd arm
  2. openbsd 386 openbsd amd64 plan9 386 plan9 amd64 windows 386

    windows amd64 リモートリポジトリー(一覧は抜粋) Command go - Remote import paths http://golang.org/cmd/go/#hdr-Remote_import_paths Bitbucket (Git, Mercurial) import "bitbucket.org/user/project" GitHub (Git) import "github.com/user/project" Google Code Project Hosting (Git, Mercurial, Subversion) import "code.google.com/p/project" Launchpad (Bazaar) import "launchpad.net/project" import "launchpad.net/~user/project/branch" ワークスペースのディレクトリーツリー $HOME +--gocode/ +--bin/ | +--hello +--pkg/ | +--$GOOS_$GOARCH/ | +--github.com/ | +--nf/ | +--string/ | +--string.a +--src/ +--github.com/ +--nf/ +--hello/ | +--hello.go +--string/ +--string.go +--string_test.go
  3. 実際にやってみよう! プロンプトの設定 export PS1='\w$ ' YouTube 1 ~$ ~$ mkdir

    gocode ~$ export GOPATH=$HOME/gocode ~$ cd gocode ~/gocode$ mkdir -p src/github.com/nf ~/gocode$ cd src/github.com/nf ~/gocode/src/github.com/nf$ mkdir hello ~/gocode/src/github.com/nf$ cd hello ~/gocode/src/github.com/nf/hello$ vim hello.go hello.go package main import "fmt" func main() { fmt.Println("Hello, new gopher!") } YouTube 2 ~/gocode/src/github.com/nf/hello$ go install ~/gocode/src/github.com/nf/hello$ ls ~/gocode/bin hello ~/gocode/src/github.com/nf/hello$ ~/gocode/bin/hello Hello, new gopher! ~/gocode/src/github.com/nf/hello$ export PATH=$HOME/gocode/bin:$PATH ~/gocode/src/github.com/nf/hello$ hello Hello, new gopher! ~/gocode/src/github.com/nf/hello$ cd .. ~/gocode/src/github.com/nf$ mkdir string ~/gocode/src/github.com/nf$ cd string ~/gocode/src/github.com/nf/string$ vim string.go string.go package string func Reverse(s string) string { b := []byte(s)
  4. for i := 0; i < len(b)/2; i++ { j

    := len(b) - i - 1 b[i], b[j] = b[j], b[i] } return string(b) } YouTube 3 ~/gocode/src/github.com/nf/string$ go build ~/gocode/src/github.com/nf/string$ go install ~/gocode/src/github.com/nf/string$ ls ~/gocode/pkg/darwin_amd64/github.com/nf string.a ~/gocode/src/github.com/nf/string$ cd ../hello ~/gocode/src/github.com/nf/hello$ vim hello.go hello.go package main import ( "fmt" "github.com/nf/string" ) func main() { fmt.Println(string.Reverse("Hello, new gopher!")) } YouTube 4 ~/gocode/src/github.com/nf/hello$ go install ~/gocode/src/github.com/nf/hello$ hello !rehpog wen ,olleH ~/gocode/src/github.com/nf/hello$ cd ../string ~/gocode/src/github.com/nf/string$ vim string_test.go string_test.go package string import "testing" func Test(t *testing.T) { var tests = []struct { s, want string }{ {"Backward", "drawkcaB"}, {"Hello, 世界", "界世 ,olleH"}, {"", ""}, }
  5. for _, c := range tests { got := Reverse(c.s)

    if got != c.want { t.Errorf("Reverse(%q) == %q, want %q", c.s, got, c.want) } } } YouTube 5 ~/gocode/src/github.com/nf/string$ go test --- FAIL: Test (0.00 seconds) string_test.go:16: Reverse("Hello, 世界") == "\x8c\x95疸 \xe4 ,olleH", want "界世 ,olleH" FAIL exit status 1 FAIL github.com/nf/string 0.012s ~/gocode/src/github.com/nf/string$ vim string.go string.go package string func Reverse(s string) string { b := []rune(s) for i := 0; i < len(b)/2; i++ { j := len(b) - i - 1 b[i], b[j] = b[j], b[i] } return string(b) } YouTube 6 ~/gocode/src/github.com/nf/string$ go test PASS ok github.com/nf/string 0.013s