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

An Introduction To Programming In Go

An Introduction To Programming In Go

Singapore Gophers Meetup - May 21, 2014

Peter Jihoon Kim

May 21, 2014
Tweet

More Decks by Peter Jihoon Kim

Other Decks in Programming

Transcript

  1. can go oredi anot? singapore gophers meetup - 21 may

    2014 an introduction to programming in go
  2. be an easier and safer alternative to C created by

    google, designed to go is a programming language
  3. built-in support for concurrency no manual memory management (gc) string,

    slice, map, and function closures methods and interfaces for structs type safety. compiled to machine code
  4. func herro(name string) string { return "herro " + name

    } ! ! func Bai(name string) string { return "okthxbai " + name } private to this package exported (public)
  5. func herro(name string) (h string) {
 h = "herro "

    + name return } named result
 parameters
  6. func herrobai(name string) (string, string) { herro := "herro "

    + name bai := "okthxbai " + name return herro, bai } ! h, b := herrobai("fren") _, b := herrobai("fren") multiple return values unused return value
  7. s = append(s, "qux", "quux") 
 t := []string{"satu", "dua"}

    s = append(s, t...) s := []string{"foo", "bar", "baz"} converts slice into
 arguments
  8. for i, v := range s { fmt.Println(i, v) }

    ! 0 foo 1 bar 2 baz s := []string{"foo", "bar", "baz"} index value
  9. for k, v := range m { fmt.Println(k, v) }

    ! foo 1 bar 2 m := map[string]int{"foo": 1, "bar":2} key value
  10. go foo() invokes function in a goroutine* * a lightweight

    thread of execution that can run concurrently
  11. sum := func(s []int, c chan int) { sum :=

    0 for _, v := range s { sum += v } c <- sum } ! a := [][]int{{1, 2, 3}, {4, 5, 6}} c := make(chan int) go sum(a[0], c) go sum(a[1], c) x, y := <-c, <-c fmt.Println(x+y) send result to channel* receive result from channel * a typed pipe through which goroutines can communicate
  12. package main ! import ( "fmt" "time" ) ! func

    main() { fmt.Println(sleepSort([]int{5, 3, 7, 10, 6, 1, 4, 2})) } ! func sleepSort(s []int) (sorted []int) { c := make(chan int) sorted = make([]int, len(s)) for _, v := range s { go func(n int) { time.Sleep(time.Duration(n) * time.Second) c <- n }(v) } for i := 0; i < len(s); i++ { sorted[i] = <-c } return }
  13. p := Person{Name: "Pete", Age: 28} ! q := &Person{Name:

    "Pete", Age: 28} “address of” type: Person type: *Person (pointer to Person)
  14. p := Person{Name: "Pete", Age: 28} q := &Person{Name: "Pete",

    Age: 28} ! p2 := p ! q2 := q copies p points to *q
  15. p := Person{Name: "Pete", Age: 28} q := &Person{Name: "Pete",

    Age: 28} ! p.Name q.Name p.Age = 18 q.Age = 18 "Pete"
  16. type Person struct { Name string Age int }
 func

    (this *Person) Greet() { fmt.Println(this.Name + " says hello!") } p := &Person{Name: "Pete", Age: 28} p.Greet() Pete says hello! method declaration method receiver
  17. type Triangle struct { Base float64 Height float64 } !

    func (this *Triangle) Area() float64 { return 0.5 * this.Base * this.Height } ! type Rectangle struct { Width float64 Length float64 } ! func (this *Rectangle) Area() float64 { return this.Width * this.Length }
  18. type Shape interface { Area() float64 } ! ! shapes

    := []Shape{ &Triangle{Base: 10.0, Height: 5.0}, &Rectangle{Width: 5.0, Length: 4.0}, } ! for _, shape := range shapes { fmt.Println(shape.Area()) } interfaces are satisfied
 implicitly
  19. check out “a tour of go”: tour.golang.org create a free

    go box at www.nitrous.io where to go from here? topics not covered today: packages, testing, web development… attend future singapore gophers meetups