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

Go言語の紹介(かなり浅く浅く)(仮)(お盆開け勉強会)

2a5647093df7a0dc1c32e821be413129?s=47 Symptote
August 25, 2014

 Go言語の紹介(かなり浅く浅く)(仮)(お盆開け勉強会)

2a5647093df7a0dc1c32e821be413129?s=128

Symptote

August 25, 2014
Tweet

Transcript

  1. Goݴޠͷ঺հʢ͔ͳΓઙ͘ઙ͘ʣ(Ծ) @a0symptote

  2. ࣗݾ঺հ ໊લ : ͠Μ΀ͬͱ ೥ྸ : 20 ॴଐ : ߴઐ

    ڵຯ : ɹ ɹ਺ֶͰ࢖͏ਤ(άϥϑ౳)ͷ࡞੒
  3. ຊ೔ͷ಺༰

  4. Goݴޠͬͯʁ Google ͕։ൃͯ͠Δݴޠ γϯϓϧͳߏจͰ଎౓͕଎͍ͷ͕ಛ௃ ฒྻॲཧʹڧ͘ɼ؆୯ʹ࣮૷ग़དྷΔ webΞϓϦέʔγϣϯΛ࡞ΔͨΊͷϥΠϒϥϦ͕Ұഋ ࣈ۟ɾߏจղੳ༻ͷϥΠϒϥϦ΋Ұഋ Ϛείοτ͕ՄѪ͍ʂʂʂʂʂʂʂʂʂʂʂʂʂ ࢀߟจݙ :

    http:/ /golang.jp
  5. Կʹ࢖͑Δʁ WebΞϓϦέʔγϣϯͷ࡞੒ ΠϯλϓϦλɾίϯύΠϥͷ։ൃ ਺஋ܭࢉ etc…

  6. Կʹ࢖͑Δʁ WebΞϓϦέʔγϣϯͷ࡞੒ ΠϯλϓϦλɾίϯύΠϥͷ։ൃ ਺஋ܭࢉ etc… ͋ Ε ɼ ଞ ͷ

    ݴ ޠ Ͱ ΋ ྑ ͘ ͳ ͍……ʁ
  7. ͔֬ʹ

  8. GoݴޠΛ࢖͏ཧ༝ͬͯʁ Goݴޠ͕޷͖Ͱ޷͖Ͱͨ·Βͳ͍ ָʹฒྻॲཧΛར༻͍ͨ͠

  9. GoݴޠΛ࢖͏ཧ༝ͬͯʁ Goݴޠ͕޷͖Ͱ޷͖Ͱͨ·Βͳ͍ ָʹฒྻॲཧΛར༻͍ͨ͠ ී௨͸ͬͪ͜ʁ

  10. GoݴޠΛ࢖͏ཧ༝ͬͯʁ Goݴޠ͕޷͖Ͱ޷͖Ͱͨ·Βͳ͍ ָʹฒྻॲཧΛར༻͍ͨ͠ ී௨͸ͬͪ͜ʁ ͱ ͍ ͏ Θ ͚ Ͱ

  11. GoݴޠʹΑΔָʑฒྻॲཧ @a0symptote

  12. ָʹฒྻॲཧ͢ΔͨΊ͚ͩʹ GoݴޠΛ࢖͏ͷ͸ͪΐͬͱ……

  13. ղܾࡦ

  14. ղܾࡦ ଞͷݴޠͰॻ͍ͨιʔεΛ GoݴޠͰऔΓࠐΜͰ࢖͓͏

  15. औΓࠐΉͨΊͷํ๏

  16. औΓࠐΉͨΊͷํ๏ cgoͱ͍͏πʔϧ͕Goݴޠʹ͸༻ҙ͞Ε͍ͯΔ

  17. औΓࠐΉͨΊͷํ๏ cgoͱ͍͏πʔϧ͕Goݴޠʹ͸༻ҙ͞Ε͍ͯΔ ͓ΘΓ

  18. ͕͢͞ʹຯؾͳ͍ͷͰ…… ͲΜͳݴޠ͕࢖͑·͔͢ʁ Question C/C++ Ada Fortran Java Objective-C/C++ Answer

  19. ͕͢͞ʹຯؾͳ͍ͷͰ…… ͲΜͳݴޠ͕࢖͑·͔͢ʁ Question C/C++ Ada Fortran Java Objective-C/C++ Answer gccͰίϯύΠϧग़དྷΔݴޠͳΒԿͰ΋

  20. ࣮ࡍͷιʔεݟͯΈΑ͏ package main ! /* ! #include<math.h> int larger(int a,

    int b){return (a > b) ? a : b;} ! */ ! import "C" import "fmt" ! func main() { fmt.Println(C.larger(1, 2)) fmt.Println(C.cos(30.0 * 3.14 / 180.0)) }
  21. ࣮ࡍͷιʔεݟͯΈΑ͏ package main ! /* ! #include<math.h> int larger(int a,

    int b){return (a > b) ? a : b;} ! */ ! import "C" import "fmt" ! func main() { fmt.Println(C.larger(1, 2)) fmt.Println(C.cos(30.0 * 3.14 / 180.0)) } ͜ͷίϝϯτͷதʹ Cݴޠͷιʔε
  22. ࣮ࡍͷιʔεݟͯΈΑ͏ package main ! /* ! #include<math.h> int larger(int a,

    int b){return (a > b) ? a : b;} ! */ ! import "C" import "fmt" ! func main() { fmt.Println(C.larger(1, 2)) fmt.Println(C.cos(30.0 * 3.14 / 180.0)) } ͜ͷίϝϯτͷதʹ Cݴޠͷιʔε ͜ͷΑ͏ʹGoݴޠͷ Ұ෦ͱͯ͠ݺͼग़ͤΔɽ
  23. ࣮ࡍͷιʔεݟͯΈΑ͏ package main ! /* ! #include<math.h> int larger(int a,

    int b){return (a > b) ? a : b;} ! */ ! import "C" import "fmt" ! func main() { fmt.Println(C.larger(1, 2)) fmt.Println(C.cos(30.0 * 3.14 / 180.0)) } ͜ͷίϝϯτͷதʹ Cݴޠͷιʔε ͜ͷΑ͏ʹGoݴޠͷ Ұ෦ͱͯ͠ݺͼग़ͤΔɽ ϥΠϒϥϦ΋ݺ΂Δ
  24. CݴޠҎ֎Ͱ͸ʁ package main /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS:

    -framework Foundation z#import <Foundation/Foundation.h> #import <objc/Object.h> void fizzBuzz(int n) { for(int i = 1; i <= n; ++i) if(i % 15 == 0) NSLog(@"fizzbuzz\n"); else if(i % 5 == 0) NSLog(@"buzz\n"); else if(i % 3 == 0) NSLog(@"fizz\n"); else NSLog(@"%d\n", i); } */ import "C" ! func main() { C.fizzBuzz(20) }
  25. CݴޠҎ֎Ͱ͸ʁ package main /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS:

    -framework Foundation z#import <Foundation/Foundation.h> #import <objc/Object.h> void fizzBuzz(int n) { for(int i = 1; i <= n; ++i) if(i % 15 == 0) NSLog(@"fizzbuzz\n"); else if(i % 5 == 0) NSLog(@"buzz\n"); else if(i % 3 == 0) NSLog(@"fizz\n"); else NSLog(@"%d\n", i); } */ import "C" ! func main() { C.fizzBuzz(20) }
  26. CݴޠҎ֎Ͱ͸ʁ package main /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS:

    -framework Foundation z#import <Foundation/Foundation.h> #import <objc/Object.h> void fizzBuzz(int n) { for(int i = 1; i <= n; ++i) if(i % 15 == 0) NSLog(@"fizzbuzz\n"); else if(i % 5 == 0) NSLog(@"buzz\n"); else if(i % 3 == 0) NSLog(@"fizz\n"); else NSLog(@"%d\n", i); } */ import "C" ! func main() { C.fizzBuzz(20) } gccʹೖ͍ͬͯΕ͹ɼCFLAGS͔Β ௚઀ࢦఆͯ͠΍Ε͹೉ͳ͘։͚Δ
  27. CݴޠҎ֎Ͱ͸ʁ package main /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS:

    -framework Foundation z#import <Foundation/Foundation.h> #import <objc/Object.h> void fizzBuzz(int n) { for(int i = 1; i <= n; ++i) if(i % 15 == 0) NSLog(@"fizzbuzz\n"); else if(i % 5 == 0) NSLog(@"buzz\n"); else if(i % 3 == 0) NSLog(@"fizz\n"); else NSLog(@"%d\n", i); } */ import "C" ! func main() { C.fizzBuzz(20) } gccʹೖ͍ͬͯΕ͹ɼCFLAGS͔Β ௚઀ࢦఆͯ͠΍Ε͹೉ͳ͘։͚Δ ϩʔυ͍ͨ͠ϥΠϒϥϦ౳΋ಉ͡ ͘LDFLAGSͰࢦఆग़དྷΔ
  28. ͦ͏ݴ͑͹ฒྻॲཧ͸ʁ

  29. ͦ͏ݴ͑͹ฒྻॲཧ͸ʁ go 並列化したい関数()

  30. ͦ͏ݴ͑͹ฒྻॲཧ͸ʁ go 並列化したい関数() ͓ΘΓ

  31. ࠓճฒྻͰॲཧͯ͠ΈΔ໰୊ ԁप཰πΛٻΊΑ Question

  32. ղ͖ํ π = Arctan 0.25. ্ࣜʹ͍ͭͯɼࠨลΛTaylorల։͢Δͱ π = Σ{0.25n /

    (2n + 1)}. Answer ࢀߟจݙ : wikipedia-ԁप཰ͷྺ࢙
  33. ղ͖ํ π = Arctan 0.25. ্ࣜʹ͍ͭͯɼࠨลΛTaylorల։͢Δͱ π = Σ{0.25n /

    (2n + 1)}. Answer ࢀߟจݙ : wikipedia-ԁप཰ͷྺ࢙ ඍੵ෼ͱ͔෼͔Βͳ͍ΜͰɼ࣮ݧతʹٻΊ·͢
  34. 1ลͷ௕͕͞1ͷਖ਼ํܗͱͦͷ಺઀ԁΛ ߟ͑Δɽਖ਼ํܗ಺ʹద౰ʹ఺Λ͏ͪ ԁʹೖ͍ͬͯΔ఺ͱͦΕҎ֎ͷ఺ͷൺΛ ٻΊΔɽ ࣮ݧతʹʁ Answer CݴޠɿγϛϡϨʔτ Goݴޠɿฒྻॲཧ

  35. ۩ମྫ package main ! /* #include <stdio.h> #include <stdlib.h> #include

    <time.h> void init(){srand(time(NULL));} double pi(){ //正方形と円の比を計算する,省略 } */ import "C" import "fmt" import "runtime" ! func main() { const NCPU = 5 runtime.GOMAXPROCS(NCPU) ! const MAX = 360000 / NCPU ch := make(chan float64, NCPU) ! C.init() ! for i := 0; i < NCPU; i++ { go func() { for j := 0; j < MAX; j++ { ch <- float64(C.pi()) } }() } ! p := 0.0 for i := 0; i < MAX*NCPU; i++ { p = <-ch + p } ! fmt.Println("円周率π ≈", p/(MAX*NCPU)) }
  36. ۩ମྫ package main ! /* #include <stdio.h> #include <stdlib.h> #include

    <time.h> void init(){srand(time(NULL));} double pi(){ //正方形と円の比を計算する,省略 } */ import "C" import "fmt" import "runtime" ! func main() { const NCPU = 5 runtime.GOMAXPROCS(NCPU) ! const MAX = 360000 / NCPU ch := make(chan float64, NCPU) ! C.init() ! for i := 0; i < NCPU; i++ { go func() { for j := 0; j < MAX; j++ { ch <- float64(C.pi()) } }() } ! p := 0.0 for i := 0; i < MAX*NCPU; i++ { p = <-ch + p } ! fmt.Println("円周率π ≈", p/(MAX*NCPU)) } ϓϩηε਺ͷઃఆ
  37. ۩ମྫ package main ! /* #include <stdio.h> #include <stdlib.h> #include

    <time.h> void init(){srand(time(NULL));} double pi(){ //正方形と円の比を計算する,省略 } */ import "C" import "fmt" import "runtime" ! func main() { const NCPU = 5 runtime.GOMAXPROCS(NCPU) ! const MAX = 360000 / NCPU ch := make(chan float64, NCPU) ! C.init() ! for i := 0; i < NCPU; i++ { go func() { for j := 0; j < MAX; j++ { ch <- float64(C.pi()) } }() } ! p := 0.0 for i := 0; i < MAX*NCPU; i++ { p = <-ch + p } ! fmt.Println("円周率π ≈", p/(MAX*NCPU)) } ϓϩηε਺ͷઃఆ ฒྻॲཧ෦(ແ໊ؔ਺)
  38. ࣮ߦͷ࣌ؒͷධՁ 1ίΞͰ࣮ߦ 5ίΞͰ࣮ߦ ࣮ߦ࣌ؒ 1m2.151s 0m45.90s

  39. ࣮ߦͷ࣌ؒͷධՁ 1ίΞͰ࣮ߦ 5ίΞͰ࣮ߦ ࣮ߦ࣌ؒ 1m2.151s 0m45.90s ໿1/3ͷ࣮ߦ࣌ؒͷ࡟ݮʹ

  40. ࠓճͷ·ͱΊ Goݴޠɼ͍ͬ͢͝Մೳੑʹ͋;ΕͯΔ ଞͷݴޠͷྑ͍ॴΛͭ·Έ৯͍ͯ͠ར༻Ͱ͖Δ Goݴޠࣗମ΋؆୯ʹฒྻॲཧ͕ग़དྷͨΓͱɼதʑݟॴ͕͋Δ ͜Ε͔Βൃల͍ͯ͘͠ͷͰɼظ଴ͯ͠଴ͬͯͯԼ͍͞

  41. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠