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

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

Symptote
August 25, 2014

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

Symptote

August 25, 2014
Tweet

Other Decks in Programming

Transcript

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

    ڵຯ : ɹ ɹ਺ֶͰ࢖͏ਤ(άϥϑ౳)ͷ࡞੒
  2. ࣮ࡍͷιʔεݟͯΈΑ͏ 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)) }
  3. ࣮ࡍͷιʔεݟͯΈΑ͏ 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ݴޠͷιʔε
  4. ࣮ࡍͷιʔεݟͯΈΑ͏ 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ݴޠͷ Ұ෦ͱͯ͠ݺͼग़ͤΔɽ
  5. ࣮ࡍͷιʔεݟͯΈΑ͏ 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ݴޠͷ Ұ෦ͱͯ͠ݺͼग़ͤΔɽ ϥΠϒϥϦ΋ݺ΂Δ
  6. 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) }
  7. 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) }
  8. 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͔Β ௚઀ࢦఆͯ͠΍Ε͹೉ͳ͘։͚Δ
  9. 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Ͱࢦఆग़དྷΔ
  10. ղ͖ํ π = Arctan 0.25. ্ࣜʹ͍ͭͯɼࠨลΛTaylorల։͢Δͱ π = Σ{0.25n /

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

    (2n + 1)}. Answer ࢀߟจݙ : wikipedia-ԁप཰ͷྺ࢙ ඍੵ෼ͱ͔෼͔Βͳ͍ΜͰɼ࣮ݧతʹٻΊ·͢
  12. ۩ମྫ 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)) }
  13. ۩ମྫ 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)) } ϓϩηε਺ͷઃఆ
  14. ۩ମྫ 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)) } ϓϩηε਺ͷઃఆ ฒྻॲཧ෦(ແ໊ؔ਺)