ãªããæè¿ã¿ããªæ¸ãã¦ã golang ãOSS ã¸ã®ã³ã³ããªãã¥ã¼ããã£ã³ã¹ãå¢ãã¦ãã¾ãããèªåã§ããªããªæ¸ãã«ã¯æéãèªä¿¡ããªããã§ãããã°ä¿®æ£ããããªããã£ã¦ã¿ããããããããã®ç®çã®ããã«æä½éå¿ è¦ãªç¥èãæ¸ãã¦ã¿ã¾ããã
ãã®è¨äºã§ã¯ãèªåã§ã¯ããªããªæ¸ããªãããã©ãgolang ã® OSS ããã¸ã§ã¯ãã«ã¯è²¢ç®ãããã¨ãã人åãã«ãåºæ¥ãã ãã·ã¥ãã¨å¦ã¹ãããã«éè¦ãªãã¤ã³ãã®ã¿çµã£ã¦ç´¹ä»ãã¦ããã¾ãã
åå¿è åãã®åªãã解説è¨äºã¨ãã訳ã§ã¯ãªãã®ã§ãèªåã§èª¿ã¹ãããã®ããã«ã±ã¨ãã¦ã®èªã¿æ¹ãæ³å®ãã¦ãã¾ããã¾ããèªåèªèº«ãæ³å®èªè ã®ãããééã£ããã¨ãæ¸ãã¦ããå¯è½æ§ãããã¾ããã¨ããéããæ¸ãã¦ããã¾ãã
éçºç°å¢
åºæ¬çã« Mac OSX + HomeBrew + Atom ãåæã¨ãã¾ããã¨ããã¤ã¤ãgolang ã Atom ããã¹ã¦ã®ãã©ãããã©ã¼ã ã§åãã¯ããªã®ã§ãHomeBrew ã®é¨åã ããåãã©ãããã©ã¼ã ã«ç½®ãæãã¦ããããã¨è¯ãããã§ãã
golang
$ brew install go $ echo "export GOPATH=~/.go" >> ~/.zshrc
golang ã§ã¯è¨èªèªä½ã«ããã±ã¼ã¸ããã¼ã¸ã£ã®æ©è½ããããgo get URL
ã§ããã±ã¼ã¸ãåå¾ã§ãã¾ãããã®éã« GOPATH
ã«æå®ãããã£ã¬ã¯ããªä»¥ä¸ã«é
ç½®ããã¾ããGOPATH
ã¯ç¹ã«ãã°ãã¯ãªãã好ããªå ´æãæå®ããã¨ããããã§ããRuby çåºèº«ã®å㯠rbenv ã£ã½ãæè¦ã§ ~/.go
ãæå®ãã¦ãã¾ãããghq ã使ãå§ãã¦ããã¯æ®éã®ãªãã¸ããªããã®ä¸ã«å
¥ãã¦ããã®ã§ãè¥å¹²å¤±æããæã¯å¦ãã¾ããã
Atom
ã³ã¼ããæ¸ãã¦ããæéã®ã»ã¨ãã©ã Vim ã¨ä¸ç·ã«éããã¦ããåã§ãããæ¨å¹´ã®Î²å ¬éæãã Atom ã«ç§»è¡ãã¾ãããã¾ã ãè¨å®ãã¡ã¤ã«ããç°¡åãªä¿®æ£ã®å ´å㯠Vim ã使ããã¨ãããã¾ãããåºæ¬çã«ã¯ Atom ã«æºè¶³ãã¦ãã¦ãã¡ã¤ã³ã§ä½¿ã£ã¦ãã¾ãã
golang ã®è¨èªãµãã¼ãèªä½ã¯ããã©ã«ãããã±ã¼ã¸ã¨ãã¦ä»å±ãã¦ããã®ã§ãgo-plus 㨠autocomplete-plus ã ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ apm install autocomplete-plus go-plus
ãã¡ãã Settings ããã¯ã³ã¯ãªãã¯ã§ã¤ã³ã¹ãã¼ã«ãããã¨ãåºæ¥ã¾ãã
autocomplete-plus
https://atom.io/packages/autocomplete-plus
Atom ã«ã¯ autocomplete ã¨ããè£å®ç¨ã®ããã±ã¼ã¸ãä»å±ãã¦ãã¾ããããã®ããã±ã¼ã¸ã§ã¯è£å®ã®ããã«ãã¼å
¥åãå¿
è¦ã§ããautocomplete-plus
ãã¤ã³ã¹ãã¼ã«ããã¨ãä¾ãã° .
ãå
¥åããã¨ããªã©ãè£å®ãå¯è½ãªæã¯èªåçã«åè£ã表示ãã¦ãããããã«ãªãã¾ãã
autocomplete-snippets ãå ¥ããã¨ã¹ãããããåè£ã¨ãã¦è¡¨ç¤ºãã¦ãããããã«ãªãã®ã§ããã¡ããããããã§ãã
go-plus
https://atom.io/packages/go-plus
è¨èªãµãã¼ãã§ã¯ã·ã³ã¿ãã¯ã¹ãã¤ã©ã¤ããã¹ããããã®æä¾ããã¦ãããã®ã ããã©ããã® go-plu ã§ã¯ããã« gocode
ã使ã£ãè£å®ã gofmt
ã§ã®æ´å½¢ã go-imports
ã§ã® import
ã®èªåæ¿å
¥ãªã©ãªã©ãgolang ã®ä¾¿å©ãªãã¼ã«ç¾¤ã¨ Atom ã®é£æºãå®ç¾ãã¦ããã¾ããæ£ç´ã«ããã«ã¨ã£ã¦ã¯ããããªãã¨ã³ã¼ããæ¸ããªãããã便å©ã§ãã
å ¥é
ã¨ãããã A Tour of Go ãã²ã¨éãããã°è¯ãããã§ãã åºæ¬çãªãã¨ã¯ã»ã¼å ¨ã¦ç¶²ç¾ ãã¦ããã®ã§ããããããã ã㧠golang åãä¸ããã¾ãã
ãã¨ã¯å¿ è¦ãªã¨ããã® ããã¥ã¡ã³ã ããã®ã¤ã©èªãã°ãªãã¨ããªãã¾ãã Mac ã§ããã¥ã¡ã³ãèªãã«ã¯ Dash for Mac ãããããã
ãµã³ãã«ã³ã¼ãã¯ãã¹ã¦ãã®ã¾ã¾ã³ããã§åãããã«ãã¦ãã¾ãã ãã®ããæ¬è³ªã¨é¢ä¿ã®ãªãã³ã¼ããæ¸ãã¦ããã¾ããäºæ¿ãã ããã
å®è¡æ¹æ³
ä¾ãã° sample.go
ã¨ãããã¡ã¤ã«ã«ã³ã¼ããè¨è¿°ããã¨ããã¨ã以ä¸ã®æ¹æ³ã§ã³ã³ãã¤ã«ãã¦å®è¡ã§ãã¾ãã
$ go build sample.go ./sample
å®ã¯ go run
ã使ãã¨ãã³ã³ãã¤ã«ã¨å®è¡ãåæã«ãã£ã¦ãããã®ã§ãã±ã£ã¨å®è¡ããã«ã¯ãã¡ãã®æ¹ãããããã§ãã
$ go run sample.go
ãµã¼ããã¼ãã£ã®ããã±ã¼ã¸ãåå¾ããã«ã¯ go get
ã使ãã¾ãã
ä¾ãã°ãtermbox-go ã¯ä»¥ä¸ã®ããã«ãã¦åå¾ãã¾ãã
$ go get github.com/nsf/termbox-go
å¤æ°
golang ã®å¤æ°ã¯ var å¤æ°å å
ã®ããã«å®£è¨ãããã¨ã§ãã¾ãã
ã¾ããåæååãæå®ãããã¨ã§å®£è¨ã¨åæã«å¤ã代å
¥ã§ãã¾ãã
è¤æ°ã®å¤æ°ãå®ç¾©ããå ´åã§ãåãåãå ´åã¯æå¾ã«åãæ¸ãã°å ¨ã¦ã®å¤æ°ããã®åã§ãããã¨ãæå®ã§ãã¾ãã
ãã㦠:=
ã使ããã¨ã«ãããåæ¨è«ãããããã¨ãåºæ¥ã¾ããåãæ示ããªãã¨ãããªãå ´å以å¤ã¯ããã®æ¹æ³ã§å®ç¾©ãã¦ããã°ééããªãããã§ãã
ä½è«ã§ãã :=
ããªãã¨èªããã¯è«¸èª¬ããã¾ãã(see also: := æ¼ç®åããªãã¨å¼ã¶ã®ãåé¡)
golang ã§ã¯å®ç¾©ããå¤æ°ã¯å¿
ã使ããªããã°ããããã³ã³ãã¤ã«æã«ã¨ã©ã¼ã«ãªãã¾ãã_
ã¨ããååã®å¤æ°ã ãã¯ç¹å¥ã«ãã®ã«ã¼ã«ãé©ç¨ããã¾ããã
package main import "fmt" func main() { var name string name = "Bob" fmt.Println(name) //=> Bob var name2 string = "Alice" fmt.Println(name2) //=> Alice var num, num2 int = 1, 2 fmt.Println(num, num2) //=> 1 2 isChecked := true fmt.Printf("%v", isChecked) //=> true }
å
åºæ¬åãããã¤ãããã¾ãã
- bool
- string // byte ã®ã¹ã©ã¤ã¹
- int int8 int16 int32 int64
- uint uint8 uint16 uint32 uint64 uintptr
- byte // uint8 ã®å¥å
- rune // int32 ã®å¥åãUnicode ã®ã³ã¼ããã¤ã³ãã表ã
- float32 float64
- complex64 complex128
int8
㯠8bit ã®ç¬¦å·ä»ãæ´æ°ãunit8
㯠8bit ã®ç¬¦å·ãªãæ´æ°ã§ãå¾ã¯ãã®æ³åã«å¾ãã¾ãã
uint
ã¯ç°å¢ã«ãã£ã¦ 32bit ããã㯠64bit ã®ç¬¦å·ãªãæ´æ°ã¨ãªãã¾ããint
㯠uint
ã¨åããµã¤ãºã®ç¬¦å·ä»ãæ´æ°ã§ãã
ããããuint
ã 32bit ã§ãã£ãå ´åããunit32
ã¨ã¯å¥ã®åã«ãªãã®ã§ãæ示çãªåå¤æãå¿
è¦ã§ãã
åå¤æ㯠åå(å¤)
ã¨æ¸ãã¾ãã
package main import "fmt" func main() { var a uint32 var b uint = 5 // a = b error! a = uint32(b) fmt.Println(a) //=> 5 }
ã¾ããgolang ã§ã¯æååã表ç¾ããæ¹æ³ã string
㨠[]rune
ã®2種é¡ãããåè
ã¯æåã byte
ã®ã¹ã©ã¤ã¹ã¨ãã¦æ±ããå¾è
㯠rune
ã®ã¹ã©ã¤ã¹ã¨ãã¦æ±ãã¾ãã
byte
㯠8bit ãã表ããªãã®ã«å¯¾ãã¦ãrune
㯠Unicode æåãã®ãã®ã表ãã¾ããã¾ããstring ã® range()
ãåã㨠rune
ãè¿ãããããæåãæ¯ã«ã«ã¼ããåãã¨ãããã¨ãå®ç¾åºæ¥ã¦ãã¾ãã
ã¹ã©ã¤ã¹
ã range
ã«ã¤ãã¦ã¯å¾è¿°ãã¾ãã
package main import "fmt" func main() { s := "æ¥æ¬èª" r := []rune(s) fmt.Printf("%#U\n", s[0]) //=> U+00E6 'æ' fmt.Printf("%#U\n", r[0]) //=> U+65E5 'æ¥' }
é å 㨠ããã
golang ã§ã¯é
åã [ãµã¤ãº]å
ã¨æ¸ãã¾ããã¾ã {}
ã使ã£ã¦åæåãããã¨ãåºæ¥ã¾ããåæåããã¦ããªãè¦ç´ ã¯åæã«ããã©ã«ãã®åæå¤ãå
¥ã£ã¦ãã¾ãã
package main import "fmt" func main() { var array1 [5]int fmt.Println(array1) //=> [0 0 0 0 0] array1[2] = 5 fmt.Println(array1) //=> [0 0 5 0 0] array2 := [3]string{"one", "two", "three"} fmt.Println(array2) // => [one two three] fmt.Println(array2[1]) //=> two array3 := [...]string{"é¢åã ã£ãã", "ãµã¤ãºãçç¥", "ãããã¨ãåºæ¥ã"} fmt.Println(array3) // [é¢åã ã£ãã ãµã¤ãºãçç¥ ãããã¨ãåºæ¥ã] fmt.Println(len(array3)) //=> 3 }
ããããé£æ³é å㯠map ã使ã£ã¦å®ç¾©ãã¾ãã
package main import "fmt" func main() { var map1 map[string]int = map[string]int{} map1["one"] = 1 fmt.Println(map1) //=> map[one:1] fmt.Println(map1["one"]) //=> 1 map2 := map[int]bool{1: true, 2: false} fmt.Println(map2[2]) //=> false }
ã¹ã©ã¤ã¹
golang ã®é åã¯ç´æã«åãã¦ãã¤ã³ã¿ã¼ã§ã¯ãªãã¦ãå®éã«é åèªä½ã表ãã¦ãã¾ããã§ããæ¯åå¤ãã³ãã¼ããã®ã¯éå¹çãããã§ã¹ã©ã¤ã¹ãåå¨ãã¾ãã ã¹ã©ã¤ã¹ã¯ãé åã¸ã®ãã¤ã³ã¿ãé åã®ãµã¤ãºãé åã®ãã£ãã·ã㣠ãæã£ã¦ãããã¹ã©ã¤ã¹éã§ã¯é åã®ãã¼ã¿èªä½ã¯å ±æããã¾ãã (see also: Goè¨èªã®ã¹ã©ã¤ã¹ãç解ããã)
ã¹ã©ã¤ã¹ãä½ãæ¹æ³ã¯3ã¤ããã¾ãã
1ã¤ãã¯é
åããä½ãæ¹æ³ã§ s := array[:]
ã¨æ¸ãã¾ãã
2ã¤ãã¯ãªãã©ã«ã§æ¸ãæ¹æ³ã§ s := []int{1, 2, 3}
ã¨æ¸ãã¾ãã
3ã¤ã㯠make
ã使ã£ãæ¹æ³ã§ãmake([]string, 3, 5)
ã®ããã«ãµã¤ãºã¨ãã£ãã·ãã£ãæå®ã§ãã¾ãã
ãã£ãã·ãã£ãçç¥ããå ´åã¯ãµã¤ãºã¨åãé·ãã«ãªãã¾ãã
package main import "fmt" func main() { a := [...]int{1, 2, 3, 4, 5} fmt.Printf("%#v\n", a) //=> [5]int{1, 2, 3, 4, 5} s1 := a[:] fmt.Printf("%#v\n", s1) //=> []int{1, 2, 3, 4, 5} s2 := []string{"cat", "dog"} fmt.Printf("%#v\n", s2) //=> []string{"cat", "dog"} s3 := make([]bool, 2, 5) fmt.Printf("%#v\n", s3) //=> []bool{false, false} fmt.Println(len(s3)) //=> 2 fmt.Println(cap(s3)) //=> 5 s3 = s3[:cap(s3)] fmt.Println(len(s3)) //=> 5 s4 := make([]uint, 3) fmt.Println(cap(s4)) //=> 3 }
ã¾ããã¹ã©ã¤ã¹ãåã¹ã©ã¤ã¹ãããã¨ãåºæ¥ã¾ããåã¹ã©ã¤ã¹ã«ãã£ã¦ã¹ã©ã¤ã¹ã®ä¸é¨ã ããåãåºããã¨ãåºæ¥ã¾ãã åã¹ã©ã¤ã¹ããå ´åã§ãåç §ãã¦ããã¡ã¢ãªé åã¯åããããçæ¹ãå¤æ´ããã¨ã©ã¡ãã«ãå½±é¿ãããã¾ãã
åã¹ã©ã¤ã¹ã¯ ã¹ã©ã¤ã¹[éå§ä½ç½®:çµäºä½ç½®]
ã¨æ¸ãã¾ããçæ¹ãçç¥ããå ´åã¯ãããããã®ç«¯ã¾ã§ãæå®ãããã¨ã«ãªãã¾ãã
package main import "fmt" func main() { s1 := []int{1, 2, 3, 4, 5} fmt.Printf("%#v len=%d, cap=%d\n", s1, len(s1), cap(s1)) //=> []int{1, 2, 3, 4, 5} len=5, cap=5 s2 := s1[1:3] fmt.Printf("%#v len=%d, cap=%d\n", s2, len(s2), cap(s2)) //=> []int{2, 3} len=2, cap=4 fmt.Printf("%#v\n", s1[:3]) //=> []int{1, 2, 3} fmt.Printf("%#v\n", s1[2:]) //=> []int{3, 4, 5} s3 := s2[:1] s3[0] = 10 fmt.Printf("%#v\n", s2) //=> []int{10, 3} }
æ°ããã¡ã¢ãªé åã«ã¹ã©ã¤ã¹ã®ä¸é¨ãã³ãã¼ãããå ´å㯠copy
ã使ãã¾ãã
package main import "fmt" func main() { s1 := []int{1, 2, 3, 4, 5} s2 := make([]int, 2) copy(s2, s1[1:3]) //=> []int{2, 3} fmt.Printf("%#v\n", s2) s2[0] = 5 fmt.Printf("s1: %#v\n", s1) //=> s1: []int{1, 2, 3, 4, 5} fmt.Printf("s2: %#v\n", s2) //=> s2: []int{5, 3} }
å®æ°
å®æ°ã¯ const
ã使ã£ã¦å®ç¾©ãã¾ããå®æ°ã¯ æåãæååãçå½å¤ãæ´æ° ã®ã¿ãæ±ãã¾ãã
ãã ãåãæå®åºæ¥ãªãã®ã§å®æ°ã使ãããã³ã³ããã¹ãã«åããã¦åã決ã¾ãã¾ããæ¡ãã¶ãã«æ³¨æã
package main import "fmt" const ( One = 1 Two = 2 Three = 3 ) func main() { fmt.Println(One) //=> 1 fmt.Println(Two) //=> 2 fmt.Println(Three) //=> 3 }
å¤ãé£çªã«ãªã£ã¦ããå ´åã¯ã以ä¸ã®ãããªæ¸ãæ¹ãåºæ¥ã¾ãã
package main import "fmt" const ( One = iota + 1 Two Three ) func main() { fmt.Println(One) //=> 1 fmt.Println(Two) //=> 2 fmt.Println(Three) //=> 3 }
å¶å¾¡æ
if
ãfor
ãswitch` ãªã©ããªãã¿ã®ãã®ã使ãã¾ããæ¬å¼§ã¯çç¥ãã¾ãã
if
if
ã¯ã馴æã¿ã®æ¸ãæ¹ã§ãããæ¡ä»¶å¼ã®ä¸ã§å¤æ°ãå®ç¾©åºæ¥ãã®ãç¹å¾´ã§ããæ¡ä»¶å¼ã§å®ç¾©ããå¤æ°ã¯ if
ã®ã¹ã³ã¼ãã®ä¸ã ãã§æå¹ã§ãã
package main import "fmt" func main() { a := 5 if a == 5 { fmt.Println("a is 5") } else { fmt.Println("a is not 5") } //=> a is 5 if twice := a * 2; twice == 5 { fmt.Println("a * 2 = 5") } else if twice == 10 { fmt.Println("a * 2 = 10") } else { fmt.Println("otherwise") } //=> a * 2 == 10 // fmt.Println(twice) error! }
for
for
ã«ã¯ããã¤ãã®æ¸ãæ¹ãããã¾ãã
C è¨èªã®ãã㪠if åæå; æ¡ä»¶; å¶å¾¡ {}
ã®ãããªæ¸ãæ¹ãåºæ¥ã¾ãããfor æ¡ä»¶ {}
ã®ããã« while
ã®ä»£ããã«ä½¿ããã¨ãåºæ¥ã¾ããã¾ããfor {}
ã¨æ¸ãã°ç¡éã«ã¼ãã«ãªãã¾ãã
ã¾ããfor i, n := range(array) {}
ã®ããã« range()
ã¨åãããã¨ã¤ãã¬ã¼ã¿ã®ãããªãã¨ãåºæ¥ã¾ããi
ã«ã¯ 0 ãªãªã¸ã³ã®ãé£çªãn
ã«ã¯å¤ãå
¥ãã¾ãããããã _
ã¨ããååã«ãããã¨ã§çç¥ã§ãã¾ãã
package main import "fmt" func main() { for i := 0; i < 10; i++ { fmt.Println(i) } a := 10 for a >= 0 { fmt.Println(a) a-- } b := 0 for { if b > 10 { break } b++ } array := [...]int{1, 2, 3, 4, 5} for i, n := range array { fmt.Printf("%d: %d\n", i, n) } s := "æ¥æ¬èª" for _, c := range s { fmt.Printf("%#U\n", c) } }
switch
switch
ãã馴æã¿ã®æ¸ãæ¹ã§ãããè¤æ°ã®æ¡ä»¶ãæ¸ãããæ¨æºã§ã¯ãã©ã¼ã«ã¹ã«ã¼ããªããå¼ãæ¸ãããªã©ã®ç¹å¾´ãããã¾ãã
package main import "fmt" func main() { a := 2 switch a { case 1: fmt.Println("a is 1") case 2: fmt.Println("a is 2") default: fmt.Println("otherwise") } switch a { case 1, 2, 3, 4: fmt.Println("a is 1 or 2 or 3 or 4") default: fmt.Println("otherwise") } switch a { case 1: fallthrough // ãã©ã¼ã«ã¹ã«ã¼ããã case 2: fmt.Println("a is 1 or 2") } switch { case a == 1: fmt.Println("a is 1") case a == 2: fmt.Println("a is 2") } }
é¢æ°
é¢æ°ã¯ func åå(å¼æ°) æ»ãå¤ {}
ã¨ããæ¸ãæ¹ããã¾ããæ»ãå¤ãæå®ãã¦ããå ´å㯠return
ãå¿
é ã§ãã
è¤æ°ã®å¤ãè¿ããã¨ãåºæ¥ã¾ãã
package main import ( "fmt" "math" ) func add(a, b int) int { return a + b } func print(num int) { fmt.Println(num) } func pow(i, n int) (int, error) { if n > 10 { return 0, fmt.Errorf("n 㯠10 以ä¸ã§æå®ãã¦ãã ãã") } return int(math.Pow(float64(i), float64(n))), nil } func main() { sum := add(1, 2) print(sum) p, err := pow(10, 100) if err != nil { fmt.Println(err) } else { fmt.Printf("result: %d", p) } }
ååä»ãã®æ»ãå¤ã¨ãããã®ããã£ã¦ã以ä¸ã®ããã« sum
ããã§ã«å®ç¾©ãããç¶æ
ã¨ãã¦æ±ããreturn
ã®å¼æ°ãçç¥ã§ãã¾ãã
package main import "fmt" func add(a, b int) (sum int) { sum = a + b return } func main() { sum := add(1, 2) fmt.Println(sum) }
golang ã®é¢æ°ã¯ãã¡ã¼ã¹ãã¯ã©ã¹ãã¡ã³ã¯ã·ã§ã³ã«ãªã£ã¦ããã®ã§ãç¡åé¢æ°ãä½ã£ã¦å¤æ°ã«ä»£å ¥ãããã¨ãã§ãã¾ããã¾ããã¬ãã·ã«ã«ã¹ã³ã¼ãã§ããã®ã§ã¯ãã¼ã¸ã£ãä½ããã¨ãåºæ¥ã¾ãã
package main import "fmt" func mkCounter(init int) func() int { count := init return func() int { count++ return count } } func main() { add := func(a, b int) int { return a + b } fmt.Println(add(1, 2)) //=> 3 counter := mkCounter(5) fmt.Println(counter()) //=> 6 fmt.Println(counter()) //=> 7 fmt.Println(counter()) //=> 8 }
ã¨ã©ã¼å¦ç
è¤æ°æ»ãå¤ã®é¢æ°ã®ä¾ãæç²ãã¦åæ²ãã¾ãã
package main import ( "fmt" "math" ) func pow(i, n int) (int, error) { if n > 10 { return 0, fmt.Errorf("n 㯠10 以ä¸ã§æå®ãã¦ãã ãã") } return int(math.Pow(float64(i), float64(n))), nil } func main() { p, err := pow(10, 100) if err != nil { fmt.Println(err) } else { fmt.Printf("result: %d", p) } }
ãã®ä¾ã§ã¯ error
åã®æ»ãå¤ãè¿ãã¦ãã¾ããgolang ã§ã¯ Java ãªã©ã§ãã ä¾å¤
ãåå¨ããªãã®ã§ãèªåã§ã¨ã©ã¼ãè¿ãå¿
è¦ãããã¾ãã
å®ç¾©ããå¤æ°ã¯å¿
ã使ããªããã°ãããªãã«ã¼ã«ãããããããã®æ¹æ³ã§ã¨ã©ã¼ãè¿ãã¨å¿
ãã¨ã©ã¼å¦çãããå¿
è¦ããããããå®å
¨ãªã³ã¼ãã«ãªããã¨ãæå¾
åºæ¥ã¾ãã
æ¨æºã©ã¤ãã©ãªã®é¢æ°ãåæ§ã®æ¹æ³ã§ã¨ã©ã¼ãè¿ãã¾ãã
æ§é ä½
golang ã«ã¯ã¯ã©ã¹ã®æ¦å¿µãããã¾ããã代ããã«æ§é ä½ã使ã£ã¦ãªãã¸ã§ã¯ãã®ãããªãã¨ãå®ç¾ãã¾ãã
æ§é ä½ã¯ type åå struct {}
ã¨ããæ¸ãæ¹ã§å®ç¾©ã§ããããã¤ãã®ãã£ã¼ã«ããæã¤ãã¨ãã§ãã¾ãã
A{1, 2}
ã®ããã«åæåãã¦ãåãã£ã¼ã«ãã«ã¯ a.X
ã®ããã« .
ã使ã£ã¦ã¢ã¯ã»ã¹ã§ãã¾ãã
package main import "fmt" type A struct { X int Y int } func main() { a := A{1, 2} fmt.Printf("X: %d\n", a.X) //=> X: 1 fmt.Printf("Y: %d\n", a.Y) //=> Y: 2 a.X = 5 fmt.Printf("%#v\n", a) //=> main.A{X:5, Y:2} }
ã¡ã½ãããå®ç¾©ãããã¨ãåºæ¥ã¾ããå°ãç¬ç¹ãªã®ã§æåã¯æ°æã¡æªãããããã¾ãããããã®ãã¡æ £ãã¾ãã
åºæ¬ã¯ func (æ§é ä½) ã¡ã½ããå(å¼æ°) æ»ãå¤ {}
ã®ããã«æ¸ãã¨ãæå®ããæ§é ä½ã®ã¡ã½ãããå®ç¾©åºæ¥ã¾ãã
ã¾ãããã§æå®ããå¤æ°ã使ã£ã¦å¼ã³åºãå
ã®æ§é ä½ã«ã¢ã¯ã»ã¹ãããã¨ãåºæ¥ã¾ãã
ã¤ã¾ããperson.setName("Alice")
ã§ã¯ p
ã« person
ããname
ã« Alice
ãæ ¼ç´ããã¾ãã
package main import "fmt" type Person struct { name string age int } func (p Person) greet() { fmt.Printf("Hi, my name is %s, %d years old.\n", p.name, p.age) } func (p *Person) setName(name string) { p.name = name } func (p *Person) setAge(age int) { p.age = age } func main() { person := &Person{"Bob", 18} person.greet() person.setName("Alice") person.setAge(28) person.greet() }
(p Person)
㨠(p *Person)
ã¨ãã2種é¡ã®æ¸ãæ¹ãããã¦ããã®ã«æ°ä»ãããããããã£ããããã¨æãã¾ãã
golang ã«ããã¤ã³ã¿ãåå¨ãã¦ãã¦ãCè¨èªãªã©ã¨åãããã« &
ã§ã¢ãã¬ã¹ã表ãã*
ã§å¤ãåãåºãã¾ãã
æ§é ä½ã®å ´åã¯ãæ§é ä½ã®ãã¤ã³ã¿ããç´æ¥ãã£ã¼ã«ããã¡ã½ãããå¼ã³åºããããã«ãªã£ã¦ããã®ã§ãç°¡åã«ãã¤ã³ã¿ãæ±ãã¾ãã
ãã®ä¾ã§ã¯ person := &Person{"Bob", 18}
ã¨ãã¦ããã®ã§ãperson
ã«ã¯ Person
æ§é ä½ã®ã¤ã³ã¹ã¿ã³ã¹ã¸ã®ãã¤ã³ã¿ãæ ¼ç´ããã¦ãããã¨ã«ãªãã¾ãã
ã§ã¯ããªãã¡ã½ããã®å®ç¾©ã§ãã¤ã³ã¿ã¨ããã§ãªããã®ãããã®ã§ãããããgolang ã§ã¯åºæ¬çã«ã¯å¤æ¸¡ãããªããã¾ãã
ã¤ã¾ã func (p Person) ...
ã¨ããå®ç¾©ã§ã¯ãp
ã¯å¼ã³åºãå
ã®æ§é ä½ãã³ãã¼ãããã®ã渡ããã¦ãã¾ãã¾ãã
ãã£ã¦ããããã¡ã½ããã®ä¸ã§ãã£ã¼ã«ãã®å¤ãæ´æ°ããã¨ããã§ãå¼ã³åºãå
ã®æ§é ä½ã®ãã£ã¼ã«ãã¯æ´æ°ãããªãã¨ãããã¨ã«ãªã£ã¦ãã¾ãã¾ãã
ããã§ãfunc (p *Person) ...
ã¨ããæå®ããããã¨ã«ãã£ã¦ããã¤ã³ã¿ã§æ±ããã¨ã«ããã°ãåç
§æ¸¡ãã¨ãªãã®ã§ãå¼ã³åºãå
ã®ãã£ã¼ã«ãã®æ´æ°ãå®ç¾åºæ¥ãã®ã§ãã
ã¾ãã以ä¸ã®ããã«æ§é ä½ã®å®ç¾©ã®å é ã«å¥ã®æ§é ä½ã®ã¤ã³ã¹ã¿ã³ã¹ãé ç½®ãããã¨ã§ãç¶æ¿ã®ãããªãã¨ãå®ç¾ã§ãã¾ãã
package main import "fmt" type Programmer struct { language string } func (p Programmer) coding() { fmt.Printf("%s ãæ¸ãã¦ãã¾ã\n", p.language) } func newProgramer(langeage string) *Programmer { return &Programmer{langeage} } type Person struct { *Programmer name string age int } func (p Person) greet() { fmt.Printf("Hi, I'm %s, %d years old.\n", p.name, p.age) } func newPerson(name string, age int, language string) *Person { return &Person{ Programmer: newProgramer(language), name: name, age: age} } func main() { bob := newPerson("bob", 18, "Java") bob.greet() //=> Hi, I'm bob, 18 years old. bob.coding() //=> Java ãæ¸ãã¦ãã¾ã alice := newPerson("alice", 28, "Scala") alice.greet() //=> Hi, I'm alice, 28 yeard old. alice.coding() //=> Scala ãæ¸ãã¦ãã¾ã }
ã¤ã³ã¿ã¼ãã§ã¼ã¹
ããã¯ã¿ã¤ã ã¨ã¯ãä¾ãã°ããã¼ãã¼ã¨é³´ããã®ãããã¢ãã«ãã¨ã¿ãªãã¨ãã風ã«ãããã¡ã½ãããæã¤ãªãã¸ã§ã¯ããåã«ãããåããã®ã§ããã¨æ±ãææ³ã§ãã Ruby ãªã©ã®ããã«åãåå¨ããªãå ´åã¯ç°¡åã«å®ç¾åºæ¥ã¾ãããåã®ãã golang ã§ããã¯ã¿ã¤ãã®ãããªãã®ãå®ç¾ãããã¨ãã«ã¯ã©ãããã°ããã®ã§ããããã
golang ã«ã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ããä»çµã¿ãããããããç¨ããã¨ããã¯ã¿ã¤ããå®ç¾åºæ¥ã¾ãã
package main import "fmt" type Person interface { greet() } type Man struct { name string age int } func (p Man) greet() { fmt.Printf("Hi, my name is %s, %d years old.\n", p.name, p.age) } type Woman struct { name string } func (p Woman) greet() { fmt.Printf("Hi, my name is %s, 18 years old.\n", p.name) } func doGreet(person Person) { person.greet() } func main() { bob := &Man{"Bob", 18} doGreet(bob) alice := &Woman{"Alice"} doGreet(alice) }
ããã§ã¯ Person
ã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®ç¾©ãã¦ãããMan
㨠Woman
ããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ãã¦ãã¾ãã
doGreet
é¢æ°ã¯ Person
ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£
ãã¦ããæ§é ä½ãå¼æ°ã«åã£ã¦ãããMan
㨠Woman
ã©ã¡ãã®ã¤ã³ã¹ã¿ã³ã¹ãåãä»ãã¾ãã
ããã±ã¼ã¸
golang ã§ãããããããã±ã¼ã¸ã§ã®ãã¼ã ã¹ãã¼ã¹ã®ç®¡çãåºæ¥ã¾ãã
ããã¾ã§ package main
ã¨æ¸ãã¦ããã®ã¯ main
ããã±ã¼ã¸ã®ä¸ã«ã³ã¼ããæ¸ãã¦ãããã¨ã«ãªãã¾ãã
æ°ãã another
ããã±ã¼ã¸ãå°å
¥ããã¨ãã«ã¯ä»¥ä¸ã®ãããªãã£ã¬ã¯ããªæ§æã«ãã¾ãã
$ tree . . âââ another â  âââ another.go âââ main.go
another
ããã±ã¼ã¸ã¯ä»¥ä¸ã®ããã«æ¸ãã¦ã¿ã¾ãã
package another func NewA(value int) *A { return &A{value} } type A struct { value int } func (a A) Value() int { return a.value } func newb(value int) *b { return &b{value} } type b struct { value int } func (b b) Value() int { return b.value }
ããã§ã¯ãã㦠camelCase
㨠CamelCase
ãæ··ãã¦ãã¾ãã
å®ã¯ golang ã§ã¯ãã®éãã«ã¯æå³ãããã¾ãã
1æåç®ã大æåãã¤ã¾ã CamelCase
ã«ããå ´åã¯ãããã±ã¼ã¸ã®å¤é¨ã«å
¬éãããcamelCase
ã®å ´åã¯ããã±ã¼ã¸ã®ä¸ã«ç§å¿ãããã¨ããã«ã¼ã«ãããããã®å ´åã¯æ§é ä½ A
ãé¢æ° NewA
ã¯å¤é¨ã«å
¬éãããæ§é ä½ b
ãé¢æ° newB
ã¯å¤é¨ã«ã¯å
¬éããã¾ããã
ãã®ããã±ã¼ã¸ã使ãã«ã¯ä»¥ä¸ã®ããã«ç¸å¯¾ãã¹ã§ããã±ã¼ã¸ãæå®ãã¾ãã
package main import ( "fmt" "./another" ) func main() { a := another.NewA(5) fmt.Println(a.Value()) // b := newB(10) error! }
ãã ãã注æç¹ããã£ã¦ãGOPATH
以ä¸ã«é
ç½®ããã¦ããããã¸ã§ã¯ãã®å ´åã¯ç¸å¯¾ãã¹ã§ã®æå®ã¯åºæ¥ã¾ãããGOPATH
ãåºæºã¨ãã¦ãã¹ã§æå®ããå¿
è¦ãããã¾ãã
ä¾ãã° GitHub ã§ãã¹ãã£ã³ã°ãã¦ããããã¸ã§ã¯ãã®å ´åã¯ä»¥ä¸ã®ããã«ãªãã§ãããã
package main import ( "fmt" "github.com/User/Repo/another" ) func main() { a := another.NewA(5) fmt.Println(a.Value()) }
並è¡å¦ç
ãã®è¾ºãããã ãã ã説æããããããªãã¾ãã
golang ã¯è¨èªæ©è½ã¨ãã¦ä¸¦è¡å¦çããµãã¼ããã¦ãã¾ãã以ä¸ã®ããã« goroutine ã使ãã°é常ã®é¢æ°ã並è¡ãã¦å®è¡ãããã¨ãåºæ¥ã¾ãã
ä¾ã§ã¯å®ç¾©ããé¢æ°ã使ç¨ãã¦ãã¾ãããç¡åé¢æ°ã使ãäºãã§ãã¾ãããã®å ´å㯠go func() { ... }()
ã®ããã«å®ç¾©ããé¢æ°ãå¼ã³åºãã¦ããå¿
è¦ãããã¾ãã
package main import ( "fmt" "time" ) func loop(name string) { for i := 0; i < 5; i++ { fmt.Printf("%s: %d\n", name, i) time.Sleep(1) } } func main() { go loop("a") go loop("b") for i := 0; i < 5; i++ { fmt.Printf("main: %d\n", i) time.Sleep(1) } } // main: 0 // a: 0 // b: 0 // main: 1 // a: 1 // b: 1 // main: 2 // a: 2 // b: 2 // main: 3 // a: 3 // b: 3 // main: 4 // a: 4 // b: 4
並è¡å¦çã¨ã¯å°ãéãã¾ãããdefer
ã使ãã¨é¢æ°ã®å®è¡ãé
延ãããã¨ãåºæ¥ã¾ãããã¤ã¾ã§é
延ãããã¨ããã¨ãç¾å¨ã®é¢æ°ãçµäºããã¾ã§é
延ãã¾ãã
package main import "fmt" func main() { defer func() { fmt.Println("çµäºãã¾ãã") }() fmt.Println("å¦çä¸ã§ã") }
ãã®ä¾ã ã¨å ¨ãå¬ãããªãã®ã§ããããã¨ãã°é¢æ°ãçµäºããã¨ãã«ãªã½ã¼ã¹ãéæ¾ãããªã©ã®å¦çãç°¡åã«æ¸ããããé常ã«ã¹ããã§ãã
ä¸è½ã®ããã«è¦ãã goroutine ã§ãããå¹³è¡ãã¦å®è¡ãããã¨ãããã¨ã¯ãThread ã®ããã«ãªã½ã¼ã¹ã®æ±ãã«æ³¨æãå¿
è¦ã§ãã
è¤éãªãã¨ããããã¨ãã㨠sync
ãªã©ã®ããã±ã¼ã¸ã使ã£ã¦åæãåãå¿
è¦ãåºã¦ãã¾ãã(see also: Big Sky :: golang ã® sync ããã±ã¼ã¸ã®ä½¿ãæ¹)
ãã£ãã«
並è¡ã§èµ°ã£ã¦ãã goroutine éã§ãã¼ã¿ã®ããã¨ããããã«ã¯ã©ãããã°ããã®ã§ããããã
ãã¡ãããã®åé¡ã®è§£æ±ºçãè¨èªæ©è½ã¨ãã¦ç¨æããã¦ãã¾ããããã«ã¯ãã£ãã«ã使ãã¾ãã
ãã£ãã«ã¯ make
ã使ã£ã¦ make(chan å)
ã®ããã«ä½æãã¾ããæå®ããåãããåããããã¨ãåºæ¥ããã£ãã«ãçæããã¾ãã
package main import "fmt" func add(a, b int, ch chan int) { ch <- a + b } func main() { ch := make(chan int) go add(1, 5, ch) sum := <-ch fmt.Println(sum) //=> 6 close(ch) _, ok := <-ch fmt.Println(ok) //=> false }
add
é¢æ°ããã£ãã«ã使ã£ã¦æ¸ãæãã¦ã¿ã¾ãããch <- a + b
ã¨ããæ¸ãæ¹ã§ãã§ãã«ã«å¤ãéä¿¡ãã¦ãsum := <- ch
ã§ãã£ãã«ããå¤ãåãåã£ã¦ãã¾ãã
<- ch
ã¨æ¸ããæç¹ã§ããã£ãã«ããå¤ãéããã¦ããããclose(ch)
ã®ããã«ãã¦ãã£ãã«ãéããããã¾ã§å¾
ã¤ãã¨ãåºæ¥ã¾ãã
ã¾ãã2ã¤ç®ã®æ»ãå¤ãè¦ãã¨ãã£ãã«ãéãã¦ãããã©ããã確èªãããã¨ãåºæ¥ã¾ãã
range
ã使ãã°ãã§ãã«ããé£ç¶ãã¦éããã¦ããå¤ãåãåããã¨ãåºæ¥ã¾ãããã ããé©åã«ãã£ãã«ãã¯ãã¼ãºãã¦ãããªãã¨ãããããã¯ãèµ·ããã¦ããã°ã©ã ãã¯ã©ãã·ã¥ãã¦ãã¾ãã¾ãã
package main import "fmt" func main() { ch := make(chan int) go func() { for i := 0; i < 5; i++ { ch <- i } close(ch) }() for i := range ch { fmt.Println(i) } } //=> 0 //=> 1 //=> 2 //=> 3 //=> 4
select
ã使ãã¨è¤æ°ã®ãã£ãã«ãå¾
ã¡åãããã¨ãåºæ¥ã¾ããä»åã¯å¤å´ã®ã«ã¼ããæããããã«ã©ãã«ãæå®ãã¦ãã¾ãã
package main import "fmt" type Hub struct { outCh chan int quitCh chan bool } func fibonacci(count int, hub *Hub) { a, b := 1, 1 for i := 0; i < count; i++ { hub.outCh <- a a, b = b, a+b } hub.quitCh <- true } func main() { hub := &Hub{ outCh: make(chan int), quitCh: make(chan bool), } go fibonacci(10, hub) LOOP: for { select { case n := <-hub.outCh: fmt.Println(n) case <-hub.quitCh: break LOOP } } }
ãã¹ã
golang ã®ãã¹ãã¯æ¨æºæ·»ä»ã® testing
ããã±ã¼ã¸ã使ãã¾ãããã¾ãã®ã·ã³ãã«ãã«ä»ã®è¨èªããããå ´åã¯åè¨èªã®ãã¹ãã£ã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã¨ã®è½å·®ã«é©ãããããããã¾ããã
ä¾ãã°å
ã»ã©ã® another ããã±ã¼ã¸ã®ãã¹ããæ¸ãå ´åã¯ä»¥ä¸ã®ããã«ãã¾ãããã¡ã¤ã«åã ä»»æã®åå_test.go
ã¨ããå¿
è¦ãããã¾ãã
package main import ( "testing" "./another" ) func TestAnotherA(t *testing.T) { a := another.NewA(5) if a.Value() != 5 { t.Errorf("a.Value() should be 5") } }
å®è¡ã¯ go test
ã使ãã¾ãã
$ go test another_test.go ok command-line-arguments 0.003s
ç¡äºéã£ããã¨ã確èªã§ãã¾ãããå¼æ°ãçç¥ããã¨ãã¯ã«ã¬ã³ããã£ã¬ã¯ããªã® xxx_test.go
ãã¾ã¨ãã¦å®è¡ãã¦ããã¾ãã
TextXXX
ã¨ããååã®ã§ *testing.T
åã®å¼æ°ãåãã¡ã½ãããå®è¡ãã¦ããã¾ãããããã assert
ãªã©ã¯åå¨ãããif
ãªã©ã§ãã§ãã¯ãã¦ãåé¡ãããå ´å㯠t.Errorf()
ãå¼ã¶ã¨ãã¹ãã«å¤±æããã¨ããä»çµã¿ã§ãã
ãã¹ãã«ã¤ãã¦ã¯ãã®è¨äºãã¨ã¦ãåèã«ãªãã¾ããã
ã¾ã¨ã
- Atom + go-plus ãå¿«é©
- A Tour of Go ãããã¨ãã
- è£å®ãå¹ãã¾ããããã¨ã©ã¼ã¯ã³ã³ãã¤ã©ãæ¾ã£ã¦ãããã®ã§ãèªãããããã°ãªãã¨ããªã