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

DESAMIS Go Training Season 1 Day 4

DESAMIS Go Training Season 1 Day 4

デザミス社内 Go 研修 (第1期) の4日目の資料です。
今回は Go の基本構文のうち配列・スライス、マップ、構造体、メソッド、インタフェースを学びます。

Yutaka Kato

April 27, 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. ഑ྻɾεϥΠε package mai n import "fmt " func main() {

    intArray := [3]int{1, 2, 3} // ഑ྻ (ݻఆ௕ ) intSlice := []int{1, 2, 3} // εϥΠε (Մม௕ ) intSlice = append(intSlice, 4) // εϥΠεʹ஋Λ௥Ճ fmt.Println(intArray, len(intArray)) // [1 2 3] 3 fmt.Println(intSlice, len(intSlice)) // [1 2 3 4] 4 } ഑ྻ͸ݻఆ௕ɺεϥΠε͸Մม௕
 ഑ྻ͸Ίͬͨʹ࢖Θͳ͍ 
  3. εϥΠεͷߏ଄  EBUB MFO DBQ  +BOVBSZ l'FCSVBSZ l.BSDI l"QSJM

    l.BZ l+VOF l+VMZ l"VHVTU l4FQUFNCFS l0DUPCFS l/PWFNCFS l%FDFNCFS EBUB MFO DBQ Q2 := months[4:7] summer := months[6:9] MFO DBQ MFO DBQ  ڭՊॻ1ਤ
  4. NBLF package mai n import "fmt " func main() {

    s1 := make([]int, 0 ) fmt.Println(s1, cap(s1)) // [] 0 s2 := make([]int, 0, 7 ) fmt.Println(s2, cap(s2)) // [] 7 } make() Ͱॳظͷ௕͞ͱ༰ྔΛࢦఆͯ͠εϥΠεΛ࡞੒Ͱ͖Δ
 ༧Ί࠷େ༰ྔ͕෼͔͍ͬͯΔ৔߹ʹޮ཰Α͘࡞੒͢ΔͨΊʹ࢖͏͜ͱ͕͋Δ 
  5. ϑΟϧλϦϯά package mai n import "fmt " func main() {

    stations := []string{"וా", "໼ޱ౉", "෢ଂ৽ా", "Լؙࢠ", "ӏͷ໦", "প෦", "ଟຎ઒" } var sanmoji []string // جఈ഑ྻΛ࠶ར༻͢Δ৔߹: var sanmoji = stations[:0 ] for _, s := range stations { if len([]rune(s)) == 3 { sanmoji = append(sanmoji, s ) } } fmt.Println(sanmoji) // [໼ޱ౉ Լؙࢠ ӏͷ໦ ଟຎ઒ ] } filter() ͱ͔͸ͳ͍ͷͰɺ۪௚ʹ for Λճ͢ 
  6. ൓స package mai n import "fmt " func main() {

    stations := []string{"וా", "໼ޱ౉", "෢ଂ৽ా", "Լؙࢠ", "ӏͷ໦", "প෦", "ଟຎ઒" } for i, j := 0, len(stations)-1; i < j; i, j = i+1, j-1 { stations[i], stations[j] = stations[j], stations[i ] } fmt.Println(stations) // [ଟຎ઒ প෦ ӏͷ໦... ] } reverse() ͱ͔͸ͳ͍ͷͰɺ۪௚ʹ for Λճ͢ 
  7. ෳ੡ package mai n import "fmt " func main() {

    stations := []string{"וా", "໼ޱ౉", "෢ଂ৽ా", "Լؙࢠ", "ӏͷ໦", "প෦", "ଟຎ઒" } stations2 := make([]string, len(stations) ) copy(stations2, stations ) fmt.Println(stations) // [וా ໼ޱ౉ ෢ଂ৽ా... ] } ෳ੡͸ copy() Λ࢖͏ 
  8. ؒΛൈ͘ package mai n import "fmt " func main() {

    stations := []string{"וా", "໼ޱ౉", "෢ଂ৽ా", "Լؙࢠ", "ӏͷ໦", "প෦", "ଟຎ઒" } stations = append(stations[:4], stations[5:]...) // 0ʙ3 + 5ʙ 6 fmt.Println(stations) // [וా ໼ޱ౉ ෢ଂ৽ా Լؙࢠ প෦ ଟຎ઒ ] }  εϥΠεԋࢉࢠͱ append() Ͱࣗ༝ࣗࡏʂ
  9. Ϛοϓ package mai n import "fmt " func main() {

    ages := map[string]int { "alice": 31 , "charlie": 34 , } fmt.Println(ages["alice"]) // 3 1 }  make() package mai n import "fmt " func main() { ages := make(map[string]int ) ages["alice"] = 3 1 ages["charlie"] = 3 4 fmt.Println(ages["alice"]) // 3 1 } ϚοϓϦςϥϧ
  10. Ϛοϓͷ୳ࡧ  package mai n import "fmt " func main()

    { ages := map[string]int { "alice": 31 , "charlie": 34 , } for k, v := range ages { if v > 33 { fmt.Println(k, v) // charlie 3 4 } } } for + range ʹ͔͚ΔͱΩʔɾ஋Λर͏͜ͱ͕Ͱ͖Δ
 औΓग़͞ΕΔॱং͸ཪଆͰγϟοϑϧ͞Ε͍ͯΔ
  11. ηοτ  package mai n import "fmt " func main()

    { set := map[string]bool{"A": true, "B": true, "C": true } for k := range set { fmt.Println(k ) } } ηοτΛදݱ͍ͨ͠৔߹͸ɺΩʔ͚ͩ࢖͏Ϛοϓͱͯ͠࢖͏
  12. ߏ଄ମ  package mai n import "fmt " type Employee

    struct { ID in t Name strin g } func main() { var kato Employe e fmt.Println(kato.ID, kato.Name) // 0 kato = Employee{ID: 82, Name: "Ճ౻" } fmt.Println(kato.ID, kato.Name) // 82 Ճ౻ } struct{} Ͱߏ଄ମΛఆٛͰ͖Δ
 ߏ଄ମϦςϥϧͰ؆୯ʹ૊ΈཱΔ͜ͱ͕Ͱ͖Δ
  13. ߏ଄ମͷຒΊࠐΈ  package mai n import "fmt " type Point

    struct { X, Y in t } type Circle struct { Center Poin t Radius in t } func main() { c := Circle { Center: Point{X: 10, Y: 15} , Radius: 5 , } fmt.Println(c.Center.X) // 1 0 } package mai n import "fmt " type Point struct { X, Y in t } type Circle struct { Poin t Radius in t } func main() { c := Circle { Point: Point{X: 10, Y: 15} , Radius: 5 , } fmt.Println(c.X) // 1 0 } ຒΊࠐΈ ຒΊࠐΈ ແ໊ϑΟʔϧυ ஫໨ʂ
  14. ϝιου   package mai n import "fmt " type

    Employee struct { ID in t Name strin g AnnualSalary in t } func (e Employee) MonthlySalary() int { return e.AnnualSalary / 1 2 } func main() { kato := Employee{ID: 82, Name: "Ճ౻", AnnualSalary: 3000000 } fmt.Println(kato.MonthlySalary() ) } ؔ਺໊ͷલʹϨγʔόʔͱܕΛॻ͘ͱϝιουʹͳΔ Ϩγʔόʔ
  15. ϝιου   package mai n type Employee struct {

    ID in t Name strin g Projects []strin g } func (e *Employee) Assign(pj string) { e.Projects = append(e.Projects, pj ) } func main() { kato := Employee{ID: 82, Name: "Ճ౻", Projects: []string{"։ൃ"} } kato.Assign("ӡ༻") } ϙΠϯλϨγʔόʔ ม਺ͷมߋΛߦ͏ϝιου͸ϙΠϯλϨγʔόʔʹ͢Δඞཁ͕͋Δ
  16. ϝιου   package mai n import "fmt " type

    User struct { ID int6 4 password strin g } func (u User) Authenticate(password string) bool { return u.password == passwor d } func (u User) csv() string { return fmt.Sprintf("id,password\n%d,*****\n", u.ID ) } খจࣈͰ࢝·ΔϑΟʔϧυ ֎ͷύοέʔδ͔ΒӅ͍ͨ͠ϑΟʔϧυ΍ϝιου͸খจࣈ͔Β࢝ΊΔ খจࣈͰ࢝·Δϝιου
  17. ΠϯλϑΣʔε  package mai n import "fmt " type Caller

    interface { Call() strin g } type Cattle struct { ID int6 4 } func (c Cattle) Call() string { return "moo " } func main() { var c Calle r c = Cattle{ID: 123} fmt.Println(c.Call()) // "moo " } $BMMFSܕͷม਺ʹ 
 $BUUMFΛ୅ೖ͍ͯ͠Δ ಉ͡γάχνϟ
  18. *'࣮૷ͷසग़ύλʔϯ  package mai n import "fmt " type Password

    strin g // fmt.Stringer Λ࣮૷͍ͯ͠Δ func (p Password) String() string { return "***** " } func main() { fmt.Println(Password("Tokyo2020")) // **** * } Stringer ΠϯλϑΣʔεΛ࣮૷͢Ε͹ Println ౳ͰࣗಈͰద༻͞ΕΔ
  19. JOUFSGBDF\^  package mai n import "fmt " func main()

    { anySlice := []interface{}{1, "two", 3.0 } fmt.Println(anySlice) // [1 two 3 ] anyMap := map[int]interface{}{1: "string", 2: 2.0 } fmt.Println(anyMap) // map[1:string 2:2 ] } ͳΜͰ΋ೖΔܕΛѻ͍͍ͨͱ͖͸ interface{} Λ࢖͏
  20. ࿅श໰୊ w ࿅श໰୊ 1 <ϑΥϧμ໊EBZFY> w ·ͣ͸1ͷDIBSDPVOUͷࣸܦ͔Β w ࿅श໰୊ 1

    <ϑΥϧμ໊EBZFY> w 11ͷUFNQDPOWΛNBJOύοέʔ δ಺ʹ࣮૷ɺ$FMTJVTܕ͸ઌिͷ੒Ռ෺EBZ UFNQDPOWΛJNQPSU 
  21. ͕࣌ؒ༨ͬͨΒʜ w ࿅श໰୊ 1 <ϑΥϧμ໊EBZFY> w ࢀߟʹͳΔϓϩάϥϜ͸1ͷEFEVQ w ࿅श໰୊ʙ 11

    <ϑΥϧμ໊EBZFY\^> w 11ͷJOUTFUΛࣸܦ͔ͯ͠Β࢝ΊΔ w -FO 3FNPWF ͷ࣮૷͸Ϗοτૢ࡞ͷ஌͕ࣝඞཁ w ࿅श໰୊ 1 <ϑΥϧμ໊EBZFY> w 1ͷCZUFDPVOUFSΛࢀߟʹͯ͠࡞Δ 
  22. ίʔυΛॻ͖ऴ͑ͨΒ w ʮϢʔβʔ໊QSBDUJDFʯϒϥϯνͰ࡞ۀ͍ͯ͠Δ͜ͱΛ֬ೝ͍ͯͩ͘͠͞ w 74ίʔυͷҰ൪ࠨԼʹදࣔ͞Ε͍ͯ·͢ w มߋΛεςʔδ͠ɺίϛοτ͍ͯͩ͘͠͞ w 74ίʔυͷࠨϝχϡʔͷʮιʔε؅ཧʯ͔Βૢ࡞ w

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