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

Go言語を始める時の落とし穴

85a9709bbc5f0861eba898eff7217ec6?s=47 jufuku
September 28, 2019
180

 Go言語を始める時の落とし穴

85a9709bbc5f0861eba898eff7217ec6?s=128

jufuku

September 28, 2019
Tweet

Transcript

  1. Copyright © 2017 eureka, Inc. All rights reserved. 2019/9/28 (Sat)

    EngineerX.go #1 GoݴޠΛ࢝ΊΔ࣌ͷམͱ݀͠
  2. 2 ࣗݾ঺հ Copyright © 2017 eureka, Inc. All rights reserved.

    ͓͠͝ͱ  "1*Λͭ͘Δ  Իָ׆ಈʢॕʂࣾ಺όϯυ݁੒ʂʣ ण෱৳ҰʢJufuku Shinichiʣ גࣜձࣾΤ΢ϨΧ "1*5FBNॴଐ αʔόʔαΠυΤϯδχΞʢ(P+BWB1)1ʣ
  3. 50 Shades of Go: Traps, Gotchas, and Common Mistakes for

    New Golang Devs IUUQEFWTDMPVEJNNVOJUZDPNHPUDIBTBOEDPNNPONJTUBLFTJOHPHPMBOH
  4. Copyright © 2017 eureka, Inc. All rights reserved. 4 ։࢝ͷதׅހΛผͷߦʹ഑ஔͰ͖·ͤΜ

    package main import "fmt" func main() { // syntax error: unexpected semicolon or newline before { fmt.Println("hello!") }
  5. func test( i int, s string, ) { fmt.Println("hello!") }

    ʲ͜Ε͸OKʳ func main( i int, s string, ) ( Response, error, ) { fmt.Println("hello!") } ʲ͜Ε΋OKʳ
  6. func test( i int, s string // syntax error: unexpected

    newline, expecting comma or ) ) { fmt.Println("hello!") } ʲ͜Ε͸NGʳ func main( i int, s string, ) ( Response, error // syntax error: unexpected newline, expecting comma or ) ) { fmt.Println("hello!") } ʲ͜Ε΋NGʳ
  7. Copyright © 2017 eureka, Inc. All rights reserved. 7 ม਺ͷະ࢖༻

    package main import "fmt" var gvar int // not an error func main() { var one int // error, unused variable two := 2 // error, unused variable var three int // error, even though it's assigned 3 on the next line three = 3 func(unused string) { fmt.Println("Unused arg. No compile error") }("what?") }
  8. package main import "fmt" func main() { var one int

    _ = one two := 2 fmt.Println(two) var three int three = 3 one = three var four int four = four } ʲ͜ΕͳΒOKʳ
  9. Copyright © 2017 eureka, Inc. All rights reserved. 9 ୹͍ม਺એݴͰͷ୅ೖ

    package main import "fmt" type info struct { result int } func work() (int,error) { return 13,nil } func main() { var data info data.result, err := work() //error fmt.Printf("info: %+v\n",data) }
  10. package main import "fmt" type info struct { result int

    } func work() (int, error) { return 13, nil } func main() { var data info var err error data.result, err = work() if err != nil { fmt.Println(err) return } fmt.Printf("info: %+v\n", data) } ʲ͜ΕͳΒOKʳ
  11. package main import "fmt" type info struct { result int

    } func work() (int, error) { return 13, nil } func main() { res, err := work() if err != nil { fmt.Println(err) return } data := info{result: res} fmt.Printf("info: %+v\n", data) } ʲͬͪ͜ͷํ͕ΑΓεϚʔτʳ
  12. Copyright © 2017 eureka, Inc. All rights reserved. 12 ؔ਺Ҿ਺ʹ഑ྻ

    package main import "fmt" func main() { x := [3]int{1,2,3} func(arr [3]int) { arr[0] = 7 fmt.Println(arr) //prints [7 2 3] }(x) fmt.Println(x) //prints [1 2 3] (not ok if you need [7 2 3]) }
  13. package main import "fmt" func main() { x := []int{1,

    2, 3} func(arr []int) { arr[0] = 7 fmt.Println(arr) //prints [7 2 3] }(x) fmt.Println(x) //prints [7 2 3] } ʲ͜ΕͳΒOKʳ
  14. package main import "fmt" func main() { x := make([]int,

    3) x[0] = 1 x[1] = 2 x[2] = 3 func(arr []int) { arr[0] = 7 fmt.Println(arr) //prints [7 2 3] }(x) fmt.Println(x) //prints [7 2 3] } ʲ͜Ε΋OKʳ
  15. Copyright © 2017 eureka, Inc. All rights reserved. 15 “range”۟ͷڍಈ

    package main import "fmt" func main() { x := []string{"a", "b", "c"} for v := range x { fmt.Println(v) //prints 0, 1, 2 } }
  16. package main import "fmt" func main() { x := []string{"a",

    "b", “c"} for _, v := range x { fmt.Println(v) //prints a, b, c } } ʲ͜ΕͳΒOKʳ
  17. Copyright © 2017 eureka, Inc. All rights reserved. 17 “for

    range”۟ͰͷϚοϓͷॱ൪ package main import "fmt" func main() { m := map[string]int{"one": 1, "two": 2, "three": 3, "four": 4} for k, v := range m { fmt.Println(k, v) } } IUUQTQMBZHPMBOHPSHQ/-Y#+',(#
  18. Copyright © 2017 eureka, Inc. All rights reserved. 18 ʮswitchʯͷϑΥʔϧεϧʔಈ࡞

    package main import "fmt" func main() { isSpace := func(i int) bool { switch i { case 1: case 2: return true } return false } fmt.Println(isSpace(2)) //prints true (ok) fmt.Println(isSpace(1)) //prints false (not ok) }
  19. package main import "fmt" func main() { isSpace := func(i

    int) bool { switch i { case 1,2: return true } return false } fmt.Println(isSpace(2)) //prints true (ok) fmt.Println(isSpace(1)) //prints false (not ok) } ʲ͜ΕͳΒOKʳ
  20. Copyright © 2017 eureka, Inc. All rights reserved. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠