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

Introduce Go by C Programmer

3cb4e761dbdb7aebd1ffd85f752e1e3e?s=47 sonata
August 26, 2017

Introduce Go by C Programmer

3cb4e761dbdb7aebd1ffd85f752e1e3e?s=128

sonata

August 26, 2017
Tweet

Transcript

  1. Introduce Go by C Programmer Go 1.9 Release Party in

    Tokyo @sonatard
  2. ࣗݾ঺հ • @sonatard • 2006 - ֶੜ࣌୅ • ֶ෦ͷLinuxαʔόΠϯϑϥߏஙɺӡ༻ •

    2010 - ϝʔΧʔ • SystemC - ແઢLANϞδϡʔϧγϛϡϨʔλ։ൃ • Cݴޠ - RTOSͷTCP/IPϓϩτίϧελοΫ։ൃ • Java - Jenkins͓͡͞ΜɺϓϥάΠϯ։ൃ • 2014 - झຯͰGo ghq΁ͷPR͕GoσϏϡʔɻghs, go-ldapc • 2017 - ϝϧΧϦ/ι΢κ΢ • Google App Engine Go αʔόαΠυΤϯδχΞ
  3. Why not X golang? • genericsͳ͍ͷʁ • map/filterͳ͍ͷʁ • setͳ͍ͷʁ

    • ྫ֎ͳ͍ͷʁ • XXXXͳ͍ͷʁ
  4. None
  5. Why no X Go? • genericsͳ͍ͷʁ • map/filterͳ͍ͷʁ • setͳ͍ͷʁ

    • ྫ֎ͳ͍ͷʁ • ɾɾɾɾͳ͍ͷʁ CݴޠϓϩάϥϚ͸ ؾʹͳΒͳ͍ ݩʑͳ͍ map/filterͲ͜Ζ͔list΋ͳ͍
  6. Go is great for C programmer • ॆ࣮ͨ͠ඪ४ϥΠϒϥϦ • ίϯύΠϧͷ༰қ͞

    • ެࣜϑΥʔϚολʔ • ඪ४ͷϢχοτςετπʔϧ • ΫϩείϯύΠϧɺϚϧνϓϥοτϑΥʔϜରԠ • ෳ਺ฦΓ஋ʹΑΔerrno͔Βͷ୤٫ • ΠϯλʔϑΣʔεʹΑΔΦϒδΣΫτࢦ޲ͷ࣮ݱ • GC(Garbage Collection) • ελοΫ΍ώʔϓΛҙࣝ͠ͳ͍ϓϩάϥϛϯά • ੔਺࢓༷ͷ੔ཧ
  7. Go is great for C programmer • ॆ࣮ͨ͠ඪ४ϥΠϒϥϦ • ίϯύΠϧͷ༰қ͞

    • ެࣜϑΥʔϚολʔ • ඪ४ͷϢχοτςετπʔϧ • ΫϩείϯύΠϧɺϚϧνϓϥοτϑΥʔϜରԠ • ෳ਺ฦΓ஋ʹΑΔerrno͔Βͷ୤٫ • ΠϯλʔϑΣʔεʹΑΔΦϒδΣΫτࢦ޲ͷ࣮ݱ • GC(Garbage Collection) • ελοΫ΍ώʔϓΛҙࣝ͠ͳ͍ϓϩάϥϛϯά • ੔਺࢓༷ͷ੔ཧ ϙδςΟϒͳཁૉ͔͠ ͳ͍
  8. None
  9. Go is similar to C • จ๏ • ࢓༷΋CΛݩʹॻ͔Ε͍ͯͨΓ͢Δͱ͜Ζ΋ •

    ͳͥʁ
  10. Go Designer • Rob Pike • Unixͷ։ൃऀ • Ken Thompson

    • Cݴޠͷ։ൃऀ
  11. Goͷݴޠઃܭ • Go = Cݴޠ+ α - β

  12. Go = Cݴޠ + α α = ௥Ճͱมߋ • printf

    • for loop • switch • Scope • Zero Initialization • Error Handling • ޙஔه๏ • select • ະఆٛಈ࡞ • ͦͷଞ
  13. printf • fmt.Printf͍͍ΑͶ • %v, %+v, %#v ࠷ߴ

  14. for loop • Cݴޠ • Go for (i = 0;

    i < sizeof(array)/sizeof(array[0]); i++) for i, v := range array {}
  15. switch • GoͰ͸switchͷcaseʹifͱಉ༷೚ҙͷ৚݅ࣜΛॻ͘ ͜ͱ͕Ͱ͖Δɻ • CݴޠͰ͸ɺఆ਺ͷΈ a := 100 switch

    { case a > 80: fmt.Println("over 80") default: fmt.Println("under 80") }
  16. Scope • for • if // Cݴޠ(C89) int i; for

    (i = 0; i < 10; i++ ) {} // Go for i := 0; i < 10; i++ {} // Cݴޠ err = funcX() if (err < 0) {} // είʔϓ͕࿙ΕΔ // Go if err := funcX(); err != nil { // errͷείʔϓ͕ด͡Δ }
  17. Zero initialization • Cݴޠ • ؔ਺ͷϩʔΧϧม਺ɹෆఆ஋ • άϩʔόϧม਺ 0 •

    staticม਺ 0 • Go • ͢΂ͯθϩॳظԽ
  18. Error handling • CݴޠͰ͸ • ΤϥʔॲཧΛ๨ΕΔ͜ͱ͕ଟ͍ • ฦΓ஋ΛແࢹͰ͖Δɻ • intͷฦΓ஋ʹҙຯ͕͋ΔͨΊɺ஋ʹԠͨ͡໢ཏతͳΤϥʔॲཧ͕ඞཁɻ

    • ੔਺ͳΒड৴ͨ͠σʔλαΠζɺ0ະຬͳΒΤϥʔɺ0͸ɾɾɾ • Τϥʔͷৄࡉ͸ɺάϩʔόϧม਺ͷerrnoΛݟͳ͚Ε͹͍͚ͳ͍͜ͱ΋ • 1ͭͷม਺ʹෳ਺ͷҙຯ߹͍Λ͍࣋ͨͤͯͨɻΤϥʔઐ༻ͷܕ͕ͳ͍ɻ • τϨʔε৘ใ͕ͳ͍ ret = recv( fd, buf, sizeof(buf), 0 );
  19. Error handling • GoݴޠͰ͸ • ΤϥʔॲཧΛ๨ΕΔ͜ͱ͕೉͍͠ • ໌ࣔతͳແࢹ͕ඞཁ • ݁ՌͷฦΓ஋ͱΤϥʔͷ஋͸෼཭͞Ε͍ͯΔ

    • Τϥʔͷৄࡉ͸ɺerrͷ஋Ͱ֬ೝɻ • error InterfaceͰऔΓճ͕͠ྑ͍ɻ • τϨʔε৘ใ΋४ඪ४ϥΠϒϥϦͷerrorsͰ௥ՃՄೳ n, err := conn.Read(buf) n, _ := conn.Read(buf) err1 := errors.Wrap(err, "XXX Error") fmt.Printf("%v", err1)
  20. ޙஔه๏ • ͜ΕಡΊ·͔͢ʁ • Cݴޠ int (*(*a)(int (*)(int, int), int))(int,

    int)
  21. ޙஔه๏ • Go͸ಡΈ΍͍͢ • Cݴޠ • Go int (*(*a)(int (*)(int,

    int), int))(int, int) a func(func(int, int) int, int) func(int, int) int
  22. ޙஔه๏ • Cݴޠ • Go • ౴͑ • ҎԼͷΑ͏ͳؔ਺ͷม਺aΛએݴ •

    Ҿ਺1: int,intΛҾ਺ʹऔΓฦΓ஋͕intͳؔ਺ • Ҿ਺2: int • ฦΓ஋: int,intΛҾ਺ʹऔΓฦΓ஋͕intͳؔ਺ int (*(*a)(int (*)(int, int), int))(int, int) a func(func(int, int) int, int) func(int, int) int
  23. select • Goʹ͸ɺଞͷݴޠͰ͸ݟ͔͚ͳ͍selectͱ͍ ͏ߏจ͕͋Δɻ(filterͱͯ͠ͷselectͰ͸ͳ͍) • ඇಉظॲཧͷ݁ՌΛ଴ͪड͚ΔͨΊʹ࢖͏ɻ for { select {

    case ch1 <- v: fmt.Println("ch1") case ch2 <- v: fmt.Println("ch2") default: fmt.Println("default") } }
  24. select • Cݴޠʹ΋ඇಉظॲཧͷ݁ՌΛड͚औΔͨΊʹ ࢖͏selectͱ͍͏ؔ਺͕͋Δɻ • ར༻໨త͸Goͱಉ͡ɻ // આ໌ʹඞཁͳ͍ཁૉ͸ল͍͍ͯΔͷͰҎԼ͸ಈ͖·ͤΜ while (1)

    { select(maxfd+1, &fds, NULL, NULL, NULL); if (FD_ISSET(sock1, &fds)) { printf("sock1\n"); } if (FD_ISSET(sock2, &fds)) { printf("sock2\n"); } }
  25. ະఆٛಈ࡞ • Cݴޠͷະఆٛಈ࡞͸ɺίϯύΠϧΤϥʔʹͳΒͣίϯύΠϥͰ͸ ҰఆͷڍಈΛࣔ͢͜ͱ͕ଟ͍ͷͰɺόάʹؾ͕෇͖ʹ͍͘ɻ • ಉ͡ιʔείʔυͰ΋ଞͷίϯύΠϥͰ͸·ͬͨ͘ผͷڍಈΛ͢Δ Մೳੑ͕͋Δɻ • ྫ)ූ߸෇͖੔਺ͷΦʔόʔϑϩʔ •

    Cݴޠ ະఆٛ(ྫ֎ɺϥοϓΞϥ΢ϯυɺͦͷଞ͢΂ͯͷՄೳੑ) • Go ϥοϓΞϥ΢ϯυ
  26. ͦͷଞ • Ϗϧυ؆୯(gcc, ld, ar... autoconf, make...)→go build • OSґଘਏ͍(windows,

    linux, mac rtos)→ڞ௨ίʔυ • ΫϩείϯύΠϧਏ͍ˠઐ༻ίϯύΠϥˠgo build • ίϯύΠϥґଘਏ͍(gcc, arm ,extensa)→goެࣜίϯύΠϥ1͚ͭͩ
  27. Go = Cݴޠ - β β = ෆཁͳػೳͷ࡟আ • ϝϞϦΞυϨεૢ࡞

    • 3߲ԋࢉࢠ • ҉໧తܕมߋ • ੔਺֦ு
  28. ϝϞϦΞυϨεૢ࡞ • ੬ऑੑͷݪҼ • ਺ʑͷ੬ऑੑΛੜΈग़͖ͯͨ͠ɻࠓ΋ੜΈग़͠ଓ͚͍ͯΔɻ • ্هͷΑ͏ʹsize͕ݻఆͳΒϛε͸গͳ͍͕ɺύέοτΛύʔεͯ͠αΠ ζΛऔಘ͢Δ৔߹ͳͲ͸ةݥɻ • ύέοτ͕ૹ͖ͬͯͨαΠζΛ৴༻ͯ͠͸͍͚ͳ͍ɻ

    int a[5]; int b[10]; int *p = &a; // b͔Βaʹ10byteίϐʔɻ // όοϑΝΦʔόʔϑϩʔ memcpy(a, b, sizeof(b))
  29. ࡾ߲ԋࢉࢠ • if จ͕1ߦʹͳΔ • ࡾ߲ԋࢉࢠ͸গ͠ಡΈͮΒ͍ • GoͰ͸ࡾ߲ԋࢉࢠ͸࡟আ • ifͰΑ͠ɹ

    int a = (x > 1) ? 10 : 5 var a int if x > 1 { a = 10 } else { a = 5 }
  30. Cݴޠͷ҉໧తܕม׵ • ܕ͕ҟͳΔ஋ͷൺֱ // Cݴޠ int a = -1; unsigned

    int b = 1; if (a < b) { printf("a < b"); } else { printf("a > b"); } // Go var a int = -1 var b uint = 1 if a < b { fmt.Println("a < b") } else { fmt.Println("a > b") }
  31. Cݴޠͷ҉໧తܕม׵ • Cݴޠ : ූ߸ແ͠੔਺ܕΛ࣋ͭΦϖϥϯυ͕ɺଞํͷΦϖϥϯυͷ੔਺ม׵ ͷॱҐΑΓߴ͍ຢ͸౳͍͠ॱҐΛ΋ͭͳΒ͹ɺූ߸෇͖੔਺ܕΛ΋ͭΦϖ ϥϯυΛɺූ߸ແ͠੔਺ܕΛ΋ͭΦϖϥϯυͷܕʹม׵͢Δɻ • Go :

    ҉໧తܕม׵͸ͳ͍ɻܕ͕ҟͳΔ΋ͷಉ࢜ͷԋࢉ͸ίϯύΠϧΤϥʔ // Cݴޠ int a = -1; unsigned int b = 1; // int a = -1͸ɺ // unsigned intͷ4,294,967,295 // ʹม׵͞ΕΔ if (a < b) { printf("a < b"); } else { printf("a > b"); } // ग़ྗ݁Ռ a > b // Go var a int = -1 var b uint = 1 if a < b { fmt.Println("a < b") } else { fmt.Println("a > b") } // main.go:10: // invalid operation: a < b // (mismatched types int and uint)
  32. ੔਺֦ு • Cݴޠͷ੔਺֦ு • ܭࢉ్தͷ஋͕Φʔόʔϑϩʔ͢Δ͜ͱͰࢉज़Τ ϥʔ͕ى͜ΔͷΛ๷͙ short a = 32767;ɹ//

    shortͷ࠷େ஋ short b = 1; int c = a + b; // ্هͷߦ͸ҎԼͷΑ͏ʹॲཧ͞Ε·͢ɻ // int c = (int)a + (int)b short d = a + b; // ্هͷߦ͸ҎԼͷΑ͏ʹॲཧ͞Ε·͢ɻ // short d = (short)((int)a + (int)b) printf("int c = %d ",c); printf("short d = %d",d); // int c = 32768 short d = -32768
  33. ੔਺֦ு • Goݴޠͷ੔਺֦ு • ػೳͱͯ͠ଘࡏ͠ͳ͍ • Better C - Goݴޠͱ੔਺

    #golang • http://qiita.com/sonatard/items/464a9d45c689386edfe1 var a int16 = 32767 // int16ͷ࠷େ஋ var b int16 = 1 var c int32 // a + b ͸ int16Ͱܭࢉ͞ΕΔ c = int32(a + b) // ҉໧ͷܕม׵͕ͳ͍ͨΊΩϟετ͠ͳ͍ͱΤϥʔ fmt.Println(c) // -32768
  34. ·ͱΊ • Go࠷ߴ • ϓϩάϥϛϯάݴޠ Go • Alan A.A. Donovan

    B.W. Kernighan • Go͸Cݴޠ༝དྷͷ࢓༷΍ػೳ͕ଟ͍ͷͰCΛษڧ͢ΔͱಘΔ΋ͷ͕͋Δ͔΋ • ϓϩάϥϛϯάݴޠC ୈ2൛ ANSIن֨४ڌ • B.W. Kernighan , D.M. Ritchie • ϓϩάϥϛϯά࡞๏ • Brian W. Kernighan, Rob Pike
  35. ※ • ͢΂ͯͷϢʔεέʔεʹ͓͍ͯCݴޠΑΓGo͕ྑ͍Θ ͚Ͱ͸͋Γ·ͤΜ • ϋʔυ΢ΣΞͷϨδελΛૢ࡞͢Δ৔߹΍ɺϝϞϦ Λ׬શʹ؅ཧ͍ͨ͠৔߹ʹ͸Go͸޲͍͍ͯ·ͤΜ • Linux KernelɺυϥΠόͳͲ΋ಉ༷Ͱ͢

    • There's no magic
  36. Thank you!!