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

Golang で心を無にしてコードを書く

Golang で心を無にしてコードを書く

LINE Developer Meetup #11 での LT スライド。Golang の語り尽くされたであろう、単純さについて入門的にふれる話。

6/22 19:00 宮崎開催! LINE Developer Meetup #11 - connpass http://connpass.com/event/15986/

Tomoya Kose

June 22, 2015
Tweet

More Decks by Tomoya Kose

Other Decks in Programming

Transcript

  1. Golangͱ͸ Go is an open source programming language that makes

    it easy to build simple, reliable, and efficient software. The Go Programming Language https://golang.org/
  2. Golangͱ͸ Go is an open source programming language that makes

    it easy to build simple, reliable, and efficient software. The Go Programming Language https://golang.org/
  3. Golangͱ͸ Go is an open source programming language that makes

    it easy to build simple, reliable, and efficient software. The Go Programming Language https://golang.org/
  4. Golangͱ͸ Go is an open source programming language that makes

    it easy to build simple, reliable, and efficient software. The Go Programming Language https://golang.org/
  5. Golangͱ͸ Go is an open source programming language that makes

    it easy to build simple, reliable, and efficient software. The Go Programming Language https://golang.org/
  6. Golangͱ͸ Go is an open source programming language that makes

    it easy to build simple, reliable, and efficient software. The Go Programming Language https://golang.org/ γϯϓϧͰ৴པੑͷ͋Δޮ཰ͷྑ͍ιϑτ΢ΣΞͷߏஙΛָʹʂ
  7. HPSVOͱΓ͋͑ͣίϯύΠϧ࣮ͯ͠ߦ͢Δ 1 package main 2 3 import ( 4 "fmt"

    5 ) 6 7 func main() { 8 fmt.Println("Hello, world!") 9 } ίϯύΠϧɾ࣮ߦΛ͓͜ͳ͏ɻ ɾίϯύΠϧ͸ߴ଎ ɾ͍ΘΏΔ--తͳؾ࣋ͪͰ࣮ߦ
  8. HPSVOͱΓ͋͑ͣίϯύΠϧ࣮ͯ͠ߦ͢Δ 1 package main 2 3 import ( 4 "fmt"

    5 ) 6 7 func main() { 8 fmt.Println("Hello, world!") 9 } $ go run main.go ίϯύΠϧɾ࣮ߦΛ͓͜ͳ͏ɻ ɾίϯύΠϧ͸ߴ଎ ɾ͍ΘΏΔ--తͳؾ࣋ͪͰ࣮ߦ
  9. HPSVOͱΓ͋͑ͣίϯύΠϧ࣮ͯ͠ߦ͢Δ 1 package main 2 3 import ( 4 "fmt"

    5 ) 6 7 func main() { 8 fmt.Println("Hello, world!") 9 } $ go run main.go ίϯύΠϧɾ࣮ߦΛ͓͜ͳ͏ɻ ɾίϯύΠϧ͸ߴ଎ ɾ͍ΘΏΔ--తͳؾ࣋ͪͰ࣮ߦ ʮͱΓ࣮͋͑ͣߦʯ͕ؾܰʹͰ͖Δ $ go run main.go Hello, world!
  10. HPSVOͱΓ͋͑ͣίϯύΠϧ࣮ͯ͠ߦ͢Δ 1 package main 2 3 import ( 4 "fmt"

    5 ) 6 7 func main() { 8 fmt.Println("Hello, world!") 9 } $ go run main.go $ go run main.go Hello, world! ίϯύΠϧɾ࣮ߦΛ͓͜ͳ͏ɻ ɾίϯύΠϧ͸ߴ଎ ɾ͍ΘΏΔ--తͳؾ࣋ͪͰ࣮ߦ ։ൃऀͰ͋ΔRob Pikeࢯ͍Θ͘ʮಛผ଎͍Θ͚Ͱ͸ͳ͍ɻ஗͘ͳΔ͜ͱΛ΍ͬͯͳ͍͚ͩɻʯͱͷ͜ͱ ʮͱΓ࣮͋͑ͣߦʯ͕ؾܰʹͰ͖Δ https://twitter.com/Morikuma_Works/status/538961275779162112
  11. HPGNUίʔυΛ੔͑Δ 1 package main 2 3 4 import ( 5

    "fmt" 6 ) 7 8 func main(){ 9 fmt.Println( "Hello, world!") } ίʔυϑΥʔϚολʔ͕ެࣜʹఏڙ͞Ε͍ͯΔɻ ɾελΠϧͷ౷Ұͱ͍͏ ॏཁ͚ͩͲ ෆໟͳٞ࿦͕ى͖ͳ͍ɻ ɾίʔυΛॻ͘͜ͱʹूதͰ͖Δɻอଘ࣌ʹࣗಈͰϑΥʔϚοτɻ
  12. HPGNUίʔυΛ੔͑Δ 1 package main 2 3 4 import ( 5

    "fmt" 6 ) 7 8 func main(){ 9 fmt.Println( "Hello, world!") } ίʔυϑΥʔϚολʔ͕ެࣜʹఏڙ͞Ε͍ͯΔɻ $ go fmt main.go ɾελΠϧͷ౷Ұͱ͍͏ ॏཁ͚ͩͲ ෆໟͳٞ࿦͕ى͖ͳ͍ɻ ɾίʔυΛॻ͘͜ͱʹूதͰ͖Δɻอଘ࣌ʹࣗಈͰϑΥʔϚοτɻ
  13. HPGNUίʔυΛ੔͑Δ 1 package main 2 3 4 import ( 5

    "fmt" 6 ) 7 8 func main(){ 9 fmt.Println( "Hello, world!") } 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 fmt.Println("Hello, world!") 9 } ίʔυϑΥʔϚολʔ͕ެࣜʹఏڙ͞Ε͍ͯΔɻ $ go fmt main.go ɾελΠϧͷ౷Ұͱ͍͏ ॏཁ͚ͩͲ ෆໟͳٞ࿦͕ى͖ͳ͍ɻ ɾίʔυΛॻ͘͜ͱʹूதͰ͖Δɻอଘ࣌ʹࣗಈͰϑΥʔϚοτɻ
  14. HPHFUύοέʔδΛऔಘͯ͠Πϯετʔϧ $ go get -u github.com/mitsuse/bullet ύοέʔδ໊Λࢦఆ͢Δ͚ͩʂϏϧυ͔ΒΠϯετʔϧ·Ͱ׬ྃ͢Δɻ 1 import (

    2 ... 3 "github.com/codegangsta/cli" 4 "github.com/mitsuse/pushbullet-go" 5 "github.com/mitsuse/pushbullet-go/requests" 6 ) Makefile૬౰ϑΝΠϧ΍ґଘؔ܎Λఆٛ͢ΔϑΝΠϧ͕ଘࡏ͠ͳ͍ɻ
  15. HPHFUύοέʔδΛऔಘͯ͠Πϯετʔϧ $ go get -u github.com/mitsuse/bullet ύοέʔδ໊Λࢦఆ͢Δ͚ͩʂϏϧυ͔ΒΠϯετʔϧ·Ͱ׬ྃ͢Δɻ 1 import (

    2 ... 3 "github.com/codegangsta/cli" 4 "github.com/mitsuse/pushbullet-go" 5 "github.com/mitsuse/pushbullet-go/requests" 6 ) import͞ΕͯΔύοέʔδ΋औಘɾϏϧυ Makefile૬౰ϑΝΠϧ΍ґଘؔ܎Λఆٛ͢ΔϑΝΠϧ͕ଘࡏ͠ͳ͍ɻ
  16. HPHFUύοέʔδΛऔಘͯ͠Πϯετʔϧ $ go get -u github.com/mitsuse/bullet ύοέʔδ໊Λࢦఆ͢Δ͚ͩʂϏϧυ͔ΒΠϯετʔϧ·Ͱ׬ྃ͢Δɻ ґଘؔ܎ͷ؅ཧ͸ɺίʔυதͷimport͕݉Ͷ͍ͯΔɻແବ͕ͳ͍ɻ 1 import

    ( 2 ... 3 "github.com/codegangsta/cli" 4 "github.com/mitsuse/pushbullet-go" 5 "github.com/mitsuse/pushbullet-go/requests" 6 ) import͞ΕͯΔύοέʔδ΋औಘɾϏϧυ Makefile૬౰ϑΝΠϧ΍ґଘؔ܎Λఆٛ͢ΔϑΝΠϧ͕ଘࡏ͠ͳ͍ɻ
  17. ߏ଄ମ(struct)ɿ Goͷߏ଄ମ͸ϝιουΛ࣋ͭ 1 package main 2 3 import ( 4

    "fmt" 5 ) 6 7 type Cat struct { 8 name string 9 } 10 11 func (c *Cat) Name() string { 12 return c.name 13 } 14 15 func main() { 16 cat := &Cat{ 17 name: "Rentaro", 18 } 19 20 fmt.Println(cat.Name()) 21 } ߏ଄ମͷΠϯελϯε͔Βυοτ෇͖Ͱϝιουݺͼग़͕͠Մೳɻ
  18. ߏ଄ମ(struct)ɿ Goͷߏ଄ମ͸ϝιουΛ࣋ͭ 1 package main 2 3 import ( 4

    "fmt" 5 ) 6 7 type Cat struct { 8 name string 9 } 10 11 func (c *Cat) Name() string { 12 return c.name 13 } 14 15 func main() { 16 cat := &Cat{ 17 name: "Rentaro", 18 } 19 20 fmt.Println(cat.Name()) 21 } ߏ଄ମͱϑΟʔϧυ ߏ଄ମͷΠϯελϯε͔Βυοτ෇͖Ͱϝιουݺͼग़͕͠Մೳɻ
  19. ߏ଄ମ(struct)ɿ Goͷߏ଄ମ͸ϝιουΛ࣋ͭ 1 package main 2 3 import ( 4

    "fmt" 5 ) 6 7 type Cat struct { 8 name string 9 } 10 11 func (c *Cat) Name() string { 12 return c.name 13 } 14 15 func main() { 16 cat := &Cat{ 17 name: "Rentaro", 18 } 19 20 fmt.Println(cat.Name()) 21 } ߏ଄ମͱϑΟʔϧυ ߏ଄ମ͸ϝιουΛ࣋ͭ ߏ଄ମͷΠϯελϯε͔Βυοτ෇͖Ͱϝιουݺͼग़͕͠Մೳɻ
  20. ߏ଄ମ(struct)ɿ Goͷߏ଄ମ͸ϝιουΛ࣋ͭ 1 package main 2 3 import ( 4

    "fmt" 5 ) 6 7 type Cat struct { 8 name string 9 } 10 11 func (c *Cat) Name() string { 12 return c.name 13 } 14 15 func main() { 16 cat := &Cat{ 17 name: "Rentaro", 18 } 19 20 fmt.Println(cat.Name()) 21 } ߏ଄ମͱϑΟʔϧυ ߏ଄ମ͸ϝιουΛ࣋ͭ {}͔newͰੜ੒ɾॳظԽ ߏ଄ମͷΠϯελϯε͔Βυοτ෇͖Ͱϝιουݺͼग़͕͠Մೳɻ
  21. ΠϯλʔϑΣʔε (interface)ɿ ϝιουఆٛͷू·Γ 1 type Named interface { 2 Name()

    string 3 } 4 5 type Cat struct { 6 name string 7 } 8 9 func (c *Cat) Name() string { 10 return c.name 11 } .. 21 func printName(n Named) { 22 fmt.Println(n.Name()) 23 } ΠϯλʔϑΣʔεͰϝιουͷγάωνϟΛఆٛ͢Δɻ
  22. ΠϯλʔϑΣʔε (interface)ɿ ϝιουఆٛͷू·Γ 1 type Named interface { 2 Name()

    string 3 } 4 5 type Cat struct { 6 name string 7 } 8 9 func (c *Cat) Name() string { 10 return c.name 11 } .. 21 func printName(n Named) { 22 fmt.Println(n.Name()) 23 } ΠϯλʔϑΣʔε͸ϝιου܈Λఆٛ ΠϯλʔϑΣʔεͰϝιουͷγάωνϟΛఆٛ͢Δɻ
  23. ΠϯλʔϑΣʔε (interface)ɿ ϝιουఆٛͷू·Γ 1 type Named interface { 2 Name()

    string 3 } 4 5 type Cat struct { 6 name string 7 } 8 9 func (c *Cat) Name() string { 10 return c.name 11 } .. 21 func printName(n Named) { 22 fmt.Println(n.Name()) 23 } ΠϯλʔϑΣʔε͸ϝιου܈Λఆٛ *Cat͸Name()ϝιουΛ࣋ͭ ΠϯλʔϑΣʔεͰϝιουͷγάωνϟΛఆٛ͢Δɻ
  24. ΠϯλʔϑΣʔε (interface)ɿ ϝιουఆٛͷू·Γ 1 type Named interface { 2 Name()

    string 3 } 4 5 type Cat struct { 6 name string 7 } 8 9 func (c *Cat) Name() string { 10 return c.name 11 } .. 21 func printName(n Named) { 22 fmt.Println(n.Name()) 23 } ΠϯλʔϑΣʔε͸ϝιου܈Λఆٛ *Cat͸Name()ϝιουΛ࣋ͭ *Cat͸NamedΛຬ͍ͨͯ͠Δ ΠϯλʔϑΣʔεͰϝιουͷγάωνϟΛఆٛ͢Δɻ
  25. ΠϯλʔϑΣʔε (interface)ɿ ϝιουఆٛͷू·Γ 1 type Named interface { 2 Name()

    string 3 } 4 5 type Cat struct { 6 name string 7 } 8 9 func (c *Cat) Name() string { 10 return c.name 11 } .. 21 func printName(n Named) { 22 fmt.Println(n.Name()) 23 } ΠϯλʔϑΣʔε͸ϝιου܈Λఆٛ ΠϯλʔϑΣʔεͰϝιουͷγάωνϟΛఆٛ͢Δɻ ΠϯλʔϑΣʔε͕࣋ͭϝιου܈ͷ࣮૷Λɺߏ଄ମ͕͍࣋ͬͯΔɻ ߏ଄ମ͸ɺͦͷΠϯλʔϑΣʔεܕͱͯ͠ར༻Ͱ͖Δɻ ࣮૷ͯ͠Δ͜ͱΛ໌ࣔ͠ͳͯ͘ྑ͍ɻ੩తͳμοΫλΠϐϯάɻ *Cat͸Name()ϝιουΛ࣋ͭ *Cat͸NamedΛຬ͍ͨͯ͠Δ
  26. ߏ଄ମຒΊࠐΈ (struct embedding)ɿ ҠৡʹΑΔΠϯλʔϑΣʔεͷ࣮૷ύλʔϯ 1 type Named interface { 2

    Name() string 3 } 4 5 type Animal struct { 6 name string 7 } 8 9 func (a *Animal) Name() string { 10 return a.name 11 } 12 13 type Cat struct { 14 *Animal 15 } Golangʹ͸ܧঝ͸ͳ͍ɻ ߏ଄ମຒΊࠐΈͰҠৡͯ͠ɺΠϯλʔ ϑΣʔεΛຬͨ͢ύλʔϯɻ
  27. ߏ଄ମຒΊࠐΈ (struct embedding)ɿ ҠৡʹΑΔΠϯλʔϑΣʔεͷ࣮૷ύλʔϯ 1 type Named interface { 2

    Name() string 3 } 4 5 type Animal struct { 6 name string 7 } 8 9 func (a *Animal) Name() string { 10 return a.name 11 } 12 13 type Cat struct { 14 *Animal 15 } *Animal͸Name()ϝιουΛ࣋ͭ ߏ଄ମຒΊࠐΈͰҠৡͯ͠ɺΠϯλʔ ϑΣʔεΛຬͨ͢ύλʔϯɻ Golangʹ͸ܧঝ͸ͳ͍ɻ
  28. ߏ଄ମຒΊࠐΈ (struct embedding)ɿ ҠৡʹΑΔΠϯλʔϑΣʔεͷ࣮૷ύλʔϯ 1 type Named interface { 2

    Name() string 3 } 4 5 type Animal struct { 6 name string 7 } 8 9 func (a *Animal) Name() string { 10 return a.name 11 } 12 13 type Cat struct { 14 *Animal 15 } ಗ໊ϑΟʔϧυ*Animalͷϝιου͸ɺ *Catͷॴ༗෺ͷΑ͏ʹΞΫηεͰ͖Δ ߏ଄ମຒΊࠐΈͰҠৡͯ͠ɺΠϯλʔ ϑΣʔεΛຬͨ͢ύλʔϯɻ Golangʹ͸ܧঝ͸ͳ͍ɻ *Animal͸Name()ϝιουΛ࣋ͭ
  29. ߏ଄ମຒΊࠐΈ (struct embedding)ɿ ҠৡʹΑΔΠϯλʔϑΣʔεͷ࣮૷ύλʔϯ 1 type Named interface { 2

    Name() string 3 } 4 5 type Animal struct { 6 name string 7 } 8 9 func (a *Animal) Name() string { 10 return a.name 11 } 12 13 type Cat struct { 14 *Animal 15 } ߏ଄ମຒΊࠐΈͰҠৡͯ͠ɺΠϯλʔ ϑΣʔεΛຬͨ͢ύλʔϯɻ ܕʹ֊૚ؔ܎͕ͳ͍ɻΠϯλʔϑΣʔεΛຬ͍ͨͯ͠Δ͔͕ॏཁɻ Golangʹ͸ܧঝ͸ͳ͍ɻ ಗ໊ϑΟʔϧυ*Animalͷϝιου͸ɺ *Catͷॴ༗෺ͷΑ͏ʹΞΫηεͰ͖Δ *Animal͸Name()ϝιουΛ࣋ͭ