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

Introduce Go by C Programmer

sonatard
August 26, 2017

Introduce Go by C Programmer

sonatard

August 26, 2017
Tweet

More Decks by sonatard

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • @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 αʔόαΠυΤϯδχΞ
  2. Why no X Go? • genericsͳ͍ͷʁ • map/filterͳ͍ͷʁ • setͳ͍ͷʁ

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

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

    • ެࣜϑΥʔϚολʔ • ඪ४ͷϢχοτςετπʔϧ • ΫϩείϯύΠϧɺϚϧνϓϥοτϑΥʔϜରԠ • ෳ਺ฦΓ஋ʹΑΔerrno͔Βͷ୤٫ • ΠϯλʔϑΣʔεʹΑΔΦϒδΣΫτࢦ޲ͷ࣮ݱ • GC(Garbage Collection) • ελοΫ΍ώʔϓΛҙࣝ͠ͳ͍ϓϩάϥϛϯά • ੔਺࢓༷ͷ੔ཧ ϙδςΟϒͳཁૉ͔͠ ͳ͍
  5. Go = Cݴޠ + α α = ௥Ճͱมߋ • printf

    • for loop • switch • Scope • Zero Initialization • Error Handling • ޙஔه๏ • select • ະఆٛಈ࡞ • ͦͷଞ
  6. for loop • Cݴޠ • Go for (i = 0;

    i < sizeof(array)/sizeof(array[0]); i++) for i, v := range array {}
  7. 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ͷείʔϓ͕ด͡Δ }
  8. Error handling • CݴޠͰ͸ • ΤϥʔॲཧΛ๨ΕΔ͜ͱ͕ଟ͍ • ฦΓ஋ΛແࢹͰ͖Δɻ • intͷฦΓ஋ʹҙຯ͕͋ΔͨΊɺ஋ʹԠͨ͡໢ཏతͳΤϥʔॲཧ͕ඞཁɻ

    • ੔਺ͳΒड৴ͨ͠σʔλαΠζɺ0ະຬͳΒΤϥʔɺ0͸ɾɾɾ • Τϥʔͷৄࡉ͸ɺάϩʔόϧม਺ͷerrnoΛݟͳ͚Ε͹͍͚ͳ͍͜ͱ΋ • 1ͭͷม਺ʹෳ਺ͷҙຯ߹͍Λ͍࣋ͨͤͯͨɻΤϥʔઐ༻ͷܕ͕ͳ͍ɻ • τϨʔε৘ใ͕ͳ͍ ret = recv( fd, buf, sizeof(buf), 0 );
  9. 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)
  10. ޙஔه๏ • Go͸ಡΈ΍͍͢ • Cݴޠ • Go int (*(*a)(int (*)(int,

    int), int))(int, int) a func(func(int, int) int, int) func(int, int) int
  11. ޙஔه๏ • 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
  12. 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"); } }
  13. ͦͷଞ • Ϗϧυ؆୯(gcc, ld, ar... autoconf, make...)→go build • OSґଘਏ͍(windows,

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

    • 3߲ԋࢉࢠ • ҉໧తܕมߋ • ੔਺֦ு
  15. 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") }
  16. 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)
  17. ੔਺֦ு • 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
  18. ੔਺֦ு • 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
  19. ·ͱΊ • 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