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

DESAMIS Go Training Season 1 Day 3

DESAMIS Go Training Season 1 Day 3

デザミス社内 Go 研修 (第1期) の3日目の資料です。
今回は Go の基本構文のうち分岐とループ、基本データ型、関数、変数、定数、パッケージを学びます。

Yutaka Kato

April 20, 2021
Tweet

More Decks by Yutaka Kato

Other Decks in Technology

Transcript

  1. ݚमܭը ճ ೔࣍ ༧ఆ λΠτϧ ୈճ  Ր  (Pͷجຊ

    ୈճ  Ր  ؀ڥߏஙͱ(JU (JU)VCͷجຊ ୈճ  Ր  جຊߏจ ୈճ  Ր  ߏ଄ମɺεϥΠεɺϚοϓ ୈճ  Ր  ඪ४ϥΠϒϥϦ͸΍Ί͙Γ ୈճ  Ր  ϞδϡʔϧγεςϜͱ֎෦ϥΠϒϥϦ ୈճ  Ր  )FSPLV(Pೖ໳ ୈճ  Ր  "84-BNCEB(Pೖ໳ ୈճ  Ր  ฒߦॲཧϓϩάϥϛϯά ୈճ  Ր  ୯ମςετٕ๏ ୈճ  Ր  σʔλϕʔεΞΫηε ୈճ  Ր  'ZOFʹΑΔ(6*ΞϓϦέʔγϣϯ։ൃ ୈճ  Ր  ࣾ಺(PϓϩδΣΫτͷίʔυղઆ 
  2. JGFMTF package mai n import ( "fmt " "runtime "

    ) func main() { if runtime.GOOS == "windows" { fmt.Println("͋ͳͨͷ PC ͸ Windows Ͱ͢" ) } else if runtime.GOOS == "darwin" { fmt.Println("͋ͳͨͷ PC ͸ macOS Ͱ͢" ) } else { fmt.Println("͋ͳͨͷ PC ͸ Windows Ͱ΋ macOS Ͱ΋͋Γ·ͤΜ" ) } } ຊจ͕1ߦͰ΋ඞͣ೾ׅހͰ͘͘Δඞཁ͕͋Δ
 ৚݅ࣜ͸ؙׅހͰ͘͘Δඞཁͳ͠ 
  3. TXJUDI  package mai n import ( "fmt " "runtime

    " ) func main() { switch runtime.GOOS { case "windows" : fallthroug h case "darwin" : fmt.Println("͋ͳͨͷ PC ͸ Windows ·ͨ͸ macOS Ͱ͢" ) default : fmt.Println("͋ͳͨͷ PC ͸ Windows Ͱ΋ macOS Ͱ΋͋Γ·ͤΜ" ) } } fallthrough Ͱ໌ࣔతʹॻ͔ͳ͍ݶΓ͸Լʹ͸ྲྀΕͳ͍
 ($΍+BWBͷΑ͏ͳ break ࿙Εͷ৺഑͕͍Βͳ͍) 
  4. TXJUDI  package mai n import ( "fmt " "time

    " ) func main() { t := time.Now( ) switch { case t.Hour() < 12 : fmt.Println("͓͸Α͏͍͟͝·͢" ) case t.Hour() < 17 : fmt.Println("͜Μʹͪ͸" ) default : fmt.Println("͜Μ͹Μ͸" ) } } case ಺ʹ৚݅ࣜΛॻ͘͜ͱ΋Ͱ͖Δ 
  5. GPS  package mai n import "fmt" func main() {

    for i := 0; i < 3; i++ { fmt.Println(i ) } for i, v := range []int{1, 2, 3} { fmt.Println(i, v) // ΠϯσοΫε൪߸ ஋ } for k, v := range map[int]string{1: "͋", 2: "͍", 3: "͏"} { fmt.Println(k, v) // Ωʔ ஋ } } ੲͳ͕ΒͷΠςϨʔλʔͷΠϯΫϦϝϯτͷ΄͔ɺ
 range Λ࢖ͬͨ foreach తͳίʔυ΋هड़Ͱ͖Δ 
  6. GPS  package mai n func main() { var sum

    = 1 for sum < 1000 { sum += su m } var count = 0 for { count+ + if count < 1000 { brea k } } } 👉 👉 for Ͱ while తͳ͜ͱ΋ॻ͘
 ແݶϧʔϓ͸୯७ʹ for {…} 
  7. جຊσʔλܕ छྨ جຊσʔλܕ ஋ͷྫ ਅِ஋ bool true false ੔਺ int

    int8 int16 int32 int64 -3 0 100 1_234 ූ߸ͳ͠੔਺ uint uint8 uint16 uint32 uint64 uintptr 0 100 1_234 ුಈখ਺఺਺ float32 float64 3.14 1. .9 1e-2 ෳૉ਺ complex64 complex128 1+2i 3+4i όΠτ byte 0x7a 0xBadFace จࣈ rune string 'a' "ੈք" `\n` 
  8. จࣈྻ w TUSJOHͰఆٛͨ͠จࣈྻ͸όΠτྻͰදݱ͞Ε͍ͯΔ w 6OJDPEFจࣈͭͭ͸SVOFͱͯ͠ѻ͏͜ͱ͕Ͱ͖ Δ͕ɺSVOFͱͯ͠ѻ͏͜ͱΛ໌ࣔ͢Δඞཁ͕͋Δ ʜ ) F M

    M P & #  &  $ ʜ EBUB MFO )FMMP ੈքz ੈ ք var s = "Hello, ੈք " fmt.Println(len(s)) // όΠτ਺ = 1 3 fmt.Println(len([]rune(s))) // rune ਺ = 9 
  9. ԋࢉࢠ छྨ ԋࢉࢠ ࢉज़ + - * / % Ϗοτ

    & | ^ &^ << >> ൺֱ == != < <= > >= ࿦ཧ && || ! ΞυϨε & * νϟωϧ <- ୅ೖ += -= *= /= %= ++ -- = := 👈ୈճͰઆ໌ 👆ͷͪ΄Ͳઆ໌ 
  10. 1SJOUGϑΥʔϚοτ 7FSCT આ໌ %d 10ਐදهͷ੔਺ %x %o %b 16ਐදهɺ8ਐදهɺ2ਐදه %t

    true ͋Δ͍͸ false %c ϧʔϯ (Unicode ίʔυϙΠϯτ) %s จࣈྻ %q Ҿ༻ූͰғ·Εͨจࣈྻ͋Δ͍͸ϧʔϯ %v ೚ҙͷܕʹର͢ΔࣗવͳϑΥʔϚοτ %T ೚ҙͷ஋ͷܕ %% ύʔηϯτه߸ͦͷ΋ͷ (Φϖϥϯυͳ͠) 👈ศར fmt.Printf("%d %s %t\n”, 123, "Hello", true) 
  11. ؔ਺ͷجຊ package mai n import "fmt " // ύϥϝʔλʔ΋݁Ռ΋ͳ͍ؔ਺ func

    main() { fmt.Println(swap(1, 2)) // ݁Ռ: 2 1 } // 2ͭͷύϥϝʔλʔͱ2ͭͷ݁Ռ͕͋Δؔ਺ func swap(a, b int) (int, int) { return b, a } ໊લɺύϥϝʔλʔͷϦετɺ݁ՌͷϦετͰߏ੒͞ΕΔ
 main ؔ਺ͷΑ͏ʹύϥϝʔλʔ΋݁Ռ΋ͳ͍ؔ਺΋͋Δ 
  12. ໊લ෇͖ͷ݁Ռ package mai n import "fmt " func main() {

    fmt.Println(add(1, 2) ) } func add(a, b int) (result int) { result = a + b return // ΋ͪΖΜ return result Ͱ΋ྑ͍ } ݁Ռʹ΋໊લΛ෇͚Δ͜ͱ͕Ͱ͖Δ
 ͔͠΋ return ͷޙͷ݁ՌΛলུͰ͖Δ
 ͋Μ·Γྲྀߦ͍ͬͯͳ͍😅 👇 
  13. Τϥʔॲཧઓུ  w Τϥʔॲཧ͸errorܕΛฦ͢͜ͱͰ࣮ݱ͢Δ w errors.new()΍fmt.Errorf()ͳͲΛ༻͍ͯerror஋Λߏங͢Δ w ຊ౰ʹக໋తͳ৔߹͸panic()Λ࢖͏͕ɺΑ΄Ͳͷ͜ͱ͕ແ͍ݶΓ࢖͏͜ ͱ͸ͳ͍ package

    mai n import "errors " func checkArgs(args []string) (int, error) { if len(args) < 2 { return 2, errors.New("Ҿ਺Λ 1 ͭࢦఆ͍ͯͩ͘͠͞" ) } return 0, ni l } 
  14. Τϥʔॲཧઓུ  w errorΛฦؔ͢਺Λ࢖͏ͱ͖͸ɺͦΕ͕nilͰͳ͍͔ΛifจͰݕࠪ͢Δ ΠσΟΦϜΛ༻͍Δ w errorܕͷม਺໊͸errͱ͢Δ׳ྫ͕͋Δ package mai n

    import ( "fmt " "os " ) func main() { status, err := checkArgs(os.Args ) if err != nil { fmt.Println("Τϥʔ:", err ) } os.Exit(status ) } 
  15. Τϥʔॲཧઓུ  package mai n import ( "fmt " "os

    " ) func main() { if _, err := os.Stat("go.mod"); err != nil { fmt.Println("go.mod ͕͋Γ·ͤΜʂ" ) } } error Ҏ֎ͷ݁Ռʹڵຯ͕ͳ͍৔߹͸͜Μͳॻ͖ํ΋ʂ
 (if ͷ৚݅ࣜ಺ʹʮ;ʯͰؔ਺ݺͼग़͠ͷࣜΛ૊ΈࠐΈ) 
  16. ແ໊ؔ਺ package mai n import ( "fmt " "sort "

    ) func main() { stations := []string{"וా", "໼ޱ౉", "෢ଂ৽ా", "Լؙࢠ", "ӏͷ໦", "প෦", "ଟຎ઒" } sort.Slice(stations, func(i, j int) bool { return len(stations[i]) > len(stations[j]) // ௕͞Λൺֱ } ) fmt.Println(stations[0]) // ෢ଂ৽ా } w sort.Slice()ͷΑ͏ͳؔ਺Λཁٻ͢Δؔ਺Ͱ͸ɺͦͷ৔Ͱແ໊ؔ਺Λ࡞ͬ ͯ౉͢͜ͱ͕Ͱ͖Δ w ͜ͷؔ਺ͷؔ਺ఆٛ͸ҎԼͷΑ͏ʹͳ͍ͬͯΔ func Slice(x interface{}, less func(i, j int) bool) 
  17. ՄมݸҾ਺ؔ਺ package mai n import ( "fmt " "sort "

    ) func main() { fmt.Println(longestName("וా", "໼ޱ౉", "෢ଂ৽ా", "Լؙࢠ", "ӏͷ໦", "প෦", "ଟຎ઒") ) } func longestName(names ...string) string { if len(names) == 0 { return " " } sort.Slice(names, func(i, j int) bool { return len(names[i]) > len(names[j]) } ) return names[0 ] } ύϥϝʔλʔͷܕͷલʹলུه߸ʮ…ʯΛ෇͚ΔͱՄมݸҾ਺ʹͳΔ
 ࠷௿Ͱ΋1ͭͷύϥϝʔλʔΛऔΓ͍ͨ৔߹͸ݻఆͱՄมΛ૊Έ߹Θͤͯ޻෉͢Δ 
  18. ՄมݸҾ਺ؔ਺ package mai n import ( "fmt " "sort "

    ) func main() { stations := []string{"וా", "໼ޱ౉", "෢ଂ৽ా", "Լؙࢠ", "ӏͷ໦", "প෦", "ଟຎ઒" } fmt.Println(longestName(stations...) ) } εϥΠε དྷिৄ͘͠঺հ ΛՄมݸҾ਺ʹ౉͍ͨ͠৔߹͸ʮʯͰల։͢Δඞཁ͕͋Δ 
  19. ஗Ԇؔ਺ݺͼग़͠ w deferΛ༻͍Δͱؔ਺ͷऴྃ࣌ʹॲཧΛ࣮ࢪ͢Δ͜ͱ͕Ͱ͖Δ w ϦιʔεͷΫϩʔζॲཧͳͲʹศར package mai n import "fmt

    " func main() { fmt.Println("main() ͷ࠷ॳ" ) demo( ) fmt.Println("main() ͷ࠷ޙ" ) } func demo() { fmt.Println("=== demo() ͷ࠷ॳ" ) defer fmt.Println("=== defer ͷॲཧ" ) fmt.Println("=== demo() ͷ࠷ޙ" ) } 👉IUUQT QMBZHPMBOHPSHQ GY#@[#7+7 ΍ͬͯΈΑ͏ 
  20. EFGFSͷසग़ྫ package mai n import ( "fmt " "io "

    "net/http " ) func main() { resp, err := http.Get("https://checkip.amazonaws.com/" ) if err != nil { fmt.Println(err ) retur n } defer resp.Body.Close( ) body, err := io.ReadAll(resp.Body ) if err != nil { fmt.Println(err ) retur n } fmt.Println(string(body) ) } 👈 
  21. ೔ຊޠ΋࢖͑Δ package mai n import "fmt " func main() {

    var ม਺ strin g ม਺ = "ม਺ͩΑ " fmt.Println(ม਺, όΠτ਺(ม਺), จࣈ਺(ม਺) ) } func όΠτ਺(จࣈྻ string) int { return len(จࣈྻ ) } func จࣈ਺(จࣈྻ string) int { return len([]rune(จࣈྻ) ) } 👉IUUQTQMBZHPMBOHPSHQ9+EBHL-L 
  22. ໊લ෇͚ͷ׳श w ϩʔΧϧม਺΍ؔ਺ͷύϥϝʔλʔ͸୹͘͢Δͷ͕ଟ͍ w ྫi name err req ctx w

    είʔϓ͕େ͖͍ؔ਺͸ҙຯ͕े෼ʹ఻ΘΔ໊લʹ w ྫhttp.ListenAndServe() runtime.SetMutexProfileFraction() w ୯ޠͷ࿈݁͸Ωϟϝϧέʔεɺͨͩ͠಄ࣈޠ΍಄จࣈޠ͸ৗʹಉ ͡έʔε શͯେจࣈ͔શͯখจࣈ  w ྫtemplate.HTMLEscape() http.ParseHTTPVersion() 
  23. ༧໿ޠ ,FZXPSET break default func interface select case defer go

    map struct chan else goto package switch const fallthrough if range type continue for import return var ༧໿ޠͱ͸ɺߏจతʹڐ͞Ε͍ͯΔ৔ॴͰ͚ͩ࢖͑Δ༻ޠͷ͜ͱ 
 ͜ΕΒͷ༧໿ޠ͸ม਺໊΍ؔ਺໊ʹ࢖͏͜ͱ͸Ͱ͖ͳ͍ 
  24. ࣄલએݴ 1SFEFDMBSFE ఆ਺ true false iota nil ܕ int int8

    int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex128 complex64 bool byte rune string error ؔ਺ make len cap new append copy close delete complex real imag panic recover ݴޠ࢓্༷͸༧໿ޠͰ͸ͳ͍͕ɺ 
 ฆΒΘ͍͠ͷͰม਺໊΍ؔ਺໊ͳͲʹ༻͍Δͷ͸ආ͚Α͏ 
  25. ม਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { var s string // จࣈྻͷม਺એݴ var i int // ੔਺ͷએݴ fmt.Println(s, i) // ۭ "" ͱ 0 ͕ग़ྗ͞ΕΔ s, i = "foo", 3 // ͦΕͧΕͷม਺ʹ஋Λ୅ೖ (1ߦʹ·ͱΊͯΈͨ ) fmt.Println(s, i) // ஋ "foo" ͱ 3 ͕ग़ྗ͞ΕΔ } ༧໿ޠvarͱܕ໊Λࢦఆͯ͠ม਺ΛએݴͰ͖Δ 
 ఆٛͨ͠ม਺͸ͦͷϒϩοΫͷத ্ͷྫͩͱmainؔ਺಺ ͰͷΈར༻Ͱ͖Δ 
  26. ม਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { var s string = "foo" // จࣈྻม਺ s Λఆٛ͠ɺ "foo" Λ୅ೖ var i int = 3 // ੔਺ม਺ i Λఆٛ͠ɺ 3 Λ୅ೖ fmt.Println(s, i) // ஋ “foo" ͱ 3 ͕ग़ྗ͞ΕΔ } ఆٛͱ୅ೖΛಉ࣌ʹߦ͏͜ͱ΋Ͱ͖Δ 
  27. ม਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { var s = "foo" // จࣈྻม਺ s Λఆٛ͠ɺ "foo" Λ୅ೖ (ܕΛলུ ) var i = 3 // ੔਺ม਺ i Λఆٛ͠ɺ 3 Λ୅ೖ (ܕΛলུ ) fmt.Println(s, i) // ஋ "foo" ͱ 3 ͕ग़ྗ͞ΕΔ } ॳظ஋͕͋Ε͹ܕΛলུͰ͖Δ ܕਪ࿦ 
  28. ม਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { var ( s = "foo" // จࣈྻม਺ s Λఆٛ͠ɺ "foo" Λ୅ೖ (ܕΛলུ ) i = 3 // ੔਺ม਺ i Λఆٛ͠ɺ 3 Λ୅ೖ (ܕΛলུ ) ) fmt.Println(s, i) // ஋ ("foo") ͱ 3 ͕ग़ྗ͞ΕΔ } ෳ਺ͷvarΛ·ͱΊΔ͜ͱ΋Ͱ͖Δ 
  29. ม਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { s := "foo" // จࣈྻͷলུม਺એݴ i := 3 // ੔਺ͷলུએݴ fmt.Println(s, i) // ஋"foo" ͱ 3 ͕ग़ྗ͞ΕΔ } লུม਺એݴʮ:=ʯΛ࢖͑͹varΛॻ͔ͳͯ͘΋Α͍ 
  30. ఆ਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { const s = "foo" // ஋ "foo" Λ࣋ͭఆ਺ s Λఆٛ const i = 3 // ஋ 3 Λ࣋ͭఆ਺ i Λఆٛ fmt.Println(s, i) // ஋ "foo" ͱ 3 ͕ग़ྗ͞ΕΔ } ίϯύΠϧ࣌ʹ஋͕֬ఆ͍ͯ͠Δ৔߹ʹ༧໿ޠconstͰఆ਺ΛએݴͰ͖Δ 
 ਅِ஋ɺจࣈྻɺ਺஋ͷΈ࢖͑Δ ߏ଄ମ΍Ϛοϓ౳ʹ͸࢖͑ͳ͍ 
  31. ఆ਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { const ( a = 1 b c = 2 d ) fmt.Println(a, b, c, d) // ݁Ռ: 1 1 2 2 } ෳ਺ͷDPOTU͸·ͱΊΔ͜ͱ͕Ͱ͖ɺͦͷ৔߹͸ͭ໨Ҏ߱ͷ஋ͷলུ΋Ͱ͖Δ 
  32. ఆ਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { const ( a = iot a b c d ) fmt.Println(a, b, c, d) // ݁Ռ: 0 1 2 3 } iota ΠΦλ Λ࢖͏ͱ͔Β࢝·Γͣͭ૿Ճ͢Δఆ਺Λ؆୯ʹఆٛͰ͖Δ 
 (Pʹ͸ଞͷݴޠʹΑ͋͘Δྻڍମ enum ͕ͳ͍ͷͰɺ͜ΕͰ୅༻͢Δ͜ͱ͕ଟ͍ 
  33. ఆ਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { const ( _ = 1 << (10 * iota ) KiB // 1024 B MiB // 1048576 B (1024 KiB ) GiB // 1073741824 B (1024 MiB ) TiB // 1099511627776 (1024 GiB ) PiB // 1125899906842624 (1024 TiB ) EiB // 1152921504606846976 (1024 PiB ) ZiB // 1180591620717411303424 (1024 EiB ) YiB // 1208925819614629174706176 (1024 YiB ) ) fmt.Println(KiB, MiB, GiB, TiB, PiB, EiB ) fmt.Println(ZiB/1024, YiB/1024/1024 ) } iota͸ܭࢉࣜʹ૊ΈࠐΜͰར༻͢Δ͜ͱ΋Ͱ͖Δ 
  34. ఆ਺ͷએݴͱར༻  package mai n import "fmt " func main()

    { const n = 127 // ͜͜Λ 128 ʹ͢Δͱ࣍ͷߦ͸ίϯύΠϧΤϥʔ var i8 int8 = n // -128ʙ12 7 var i16 int16 = n // -32768ʙ3276 7 var i32 int32 = n // -2147483648ʙ214748364 7 var i64 int64 = n // -9223372036854775808ʙ922337203685477580 7 var ui8 uint8 = n // 0ʙ25 5 var ui16 uint16 = n // 0ʙ6553 5 var ui32 uint32 = n // 0ʙ429496729 5 var ui64 uint64 = n // 0ʙ1844674407370955161 5 fmt.Println(i8, i16, i32, i64, ui8, ui16, ui32, ui64 ) } ఆ਺͸ͦͷ஋͕දݱՄೳͳܕͰ͋Ε͹༷ʑͳܕʹ୅ೖͰ͖Δ 
  35. NBJOύοέʔδ package mai n import … package mai n import

    … package mai n import … mainύοέʔδ͸main()ؔ਺Λஔ͘͜ͱͰϓϩάϥϜͷ 
 ΤϯτϦʔϙΠϯτͱͳΔ͜ͱ͕Ͱ͖Δಛผͳύοέʔδ 
 ҟͳΔϓϩάϥϜͳΒσΟϨΫτϦ΋෼͚Δඞཁ͕͋Δ 
  36. ύοέʔδͷ഑ஔ EC UPPM NBJOHP VTFSHP QBDLBHFNBJO QBDLBHFEC GBSNHP QBDLBHFEC w

    σΟϨΫτϦ໊ͱύοέʔδ໊͸ඞͣ͠΋Ұக͍ͯ͠ͳͯ͘΋Α͍ w ΋ͪΖΜҰக͍ͯͨ͠΄͏͕Θ͔Γ΍͍͢ w NBJOύοέʔδͷσΟϨΫτϦ໊͸go buildίϚϯυͰ࣮ߦϑΝΠϧΛ࡞Δࡍ ʹ࣮ߦϑΝΠϧ໊ʹͳΔ ্ͷྫͩͱtool.exe͕Ͱ͖Δ 
  37. ͕࣌ؒ༨ͬͨΒʜ w ࿅श໰୊ 1  w ͖͞΄ͲͷEBZUFNQDPOWͱEBZDPOWUPPMʹίʔυΛॻ ͖଍͍ͯͩ͘͠͞ w ࿅श໰୊

    1  w EBZXFJHIUDPOWͱEBZMFOHUIDPOWΛ࡞ΓɺEBZ DPOWUPPMͷίʔυΛվྑ͍ͯͩ͘͠͞ w ࿅श໰୊ʙ 1  w ϑΥϧμ໊͸EBZDIFYʙEBZDIFYͱ͍ͯͩ͘͠͞ 
  38. ίʔυΛॻ͖ऴ͑ͨΒ w ʮϢʔβʔ໊QSBDUJDFʯϒϥϯνͰ࡞ۀ͍ͯ͠Δ͜ͱΛ֬ೝ͍ͯͩ͘͠͞ w 74ίʔυͷҰ൪ࠨԼʹදࣔ͞Ε͍ͯ·͢ w มߋΛεςʔδ͠ɺίϛοτ͍ͯͩ͘͠͞ w 74ίʔυͷࠨϝχϡʔͷʮιʔε؅ཧʯ͔Βૢ࡞ w

    ίʔυΛ(JU)VCʹϓογϡ͍ͯͩ͘͠͞ w 74ίʔυͷҰ൪ࠨԼͷϒϥϯν໊ͷ͙͢ӈʹ͋ΔΞΠίϯ͔Βૢ࡞ ্ਤ  w ϓϧϦΫΤετΛ࡞੒͍ͯͩ͘͠͞ w ʮιʔε؅ཧʯͷ্෦ʹΞΠίϯ͕͋Γ·͢