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

Learn Go in 15 minutes

Learn Go in 15 minutes

Nakamura, Ryotaro

August 08, 2017
Tweet

More Decks by Nakamura, Ryotaro

Other Decks in Technology

Transcript

  1. Exported Names 2 A name is exported if it begins

    with a capital letter package main import ( "fmt" "math" ) func main() { fmt.Println(math.Pi) }
  2. Consecutive named parameters 3 When consecutive parameters share a type,

    you can omit the type from all but the last func add(x, y int) int { return x + y }
  3. Named return values 4 A return statement without arguments returns

    the named return values func add(x, y int) (a int) { a = x+y return } func main() { fmt.Println(add(17, 1)) // 18 }
  4. Short variable declarations 5 Inside a function, the := can

    be used in place of a var with implicit type func main() { var i, j int = 1, 2 var a int a = 3 b := a // type inference // print 1 2 3 3 fmt.Println(i, j, a, b) }
  5. Slices are like references to arrays 6 A slice([] T)

    is a view into the elements of an array func main() { // primes is an array primes := [6]int{2, 3, 5, 7, 11, 13} // s is a slice var s []int = primes[1:4] fmt.Println(s) // 3, 5, 7 }
  6. Slice length and capacity 7 • len(s) ◦ the number

    of elements it contains • cap(s) ◦ the number of elements in the underlying array, counting from the first element in the slice s := []int{2, 3, 4}// len=3, cap=3 s = s[:0] // [], len=0, cap=3 s = s[:2] // [2,3], len=2, cap=3 s = s[1:] // [3], len=1, cap=2
  7. Methods 8 You can define methods on types. A method

    is a function with a special receiver argument type N struct { Num int } // (n N) is the receiver func (n N) Dec() int { return n.Num - 1 } func main() { var num = N{Num:2} fmt.Println(num.Dec()) // 1 }
  8. Interfaces are implemented implicitly 9 There is no “implements” keyword

    type Incrementer interface { Inc() } type N struct { Num int } func (n *N) Inc() { n.Num = n.Num + 1 } func main() { var num = N{Num:2} num.Inc() fmt.Println(num.Num) // 3 }
  9. Built-in interfaces 10 Error state is expresses with error values

    An empty interface may hold values of any type type error interface { Error() string } var a interface {} a = 1 a = “foo”
  10. Goroutines 11 A goroutine is a lightweight thread, and a

    go statement starts a new goroutine running func say(s string) { fmt.Println(s) } func main() { go say("world") say("hello") }
  11. Channels 12 Channels are a typed conduit through which you

    can send and receive values with the channel operator <- func hello(s string, c chan string) { c <- "hello " + s // send s to channel c } func main() { c := make(chan string) go hello("world", c) greeting := <-c // receive from c fmt.Println(greeting) // hello world }
  12. Defer 13 defer defers the execution of a function unitil

    the surrounding funtion returns src, err := os.Open(srcName) if err != nil { return } defer src.Close() dst, err := os.Create(dstName) if err != nil { return } defer dst.Close() return io.Copy(dst, src)
  13. Panic 14 panic creates a runtime error, and begins unwinding

    the stack of the goroutine func div(a, b float64) float64 { if b != 0 { return a / b } panic("div 0") }
  14. Recover 15 A call to recover stops the stack unwinding

    func main() { defer func() { if err := recover(); err != nil { fmt.Println( "panic was invoked”) } }() panic("panic") } // panic was invoked