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

Ready and Go!

Ready and Go!

A simple golang go through based on official/google golang team member sharing.

Shih-Yung Lee

February 08, 2013
Tweet

More Decks by Shih-Yung Lee

Other Decks in Programming

Transcript

  1. Basic build compile packages and dependencies clean remove object files

    doc run godoc on package sources env print Go environment information fix run go tool fix on packages fmt run gofmt on package sources get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet run go tool vet on packages http://golang.org/cmd/go/#hdr-Run_gofmt_on_package_sources
  2. Newbie package main import "fmt" const Hello = 'hello' func

    swap(x, y string) (string, string) { return y, x } func main() { a, b := swap(Hello, "world") // := func only, or var fmt.Println(a, b) }
  3. Newbie (loop) func main() { sum := 0 for i

    := 0; i < 10; i++ { \\for loop, no while sum += i } } for sum < 10 { sum += sum }
  4. Newbie (control) // if else func pow(x, n, lim float64)

    float64 { if v := math.Pow(x, n); v < lim { return v } else { fmt.Printf("%g >= %g\n", v, lim) } }
  5. Newbie (control) // switch, no break switch os := runtime.GOOS;

    os { case "darwin": fmt.Println("OS X") case "linux": fmt.Println("Linux") default: fmt.Println("%s", os) }
  6. Newbie (control) // switch, no condition == if else switch

    { case t.Hour() < 12: fmt.Println("morning") case t.Hour() < 17: fmt.Println("afternoon") default: fmt.Println("Evening") }
  7. Newbie (map) var m map[string]int m = make(map[string]int) m["test"] =

    123 var m = map[string]int { "test": 123, "test1": 5566, } v, ok := m["test2"] fmt.Println("Value: %s, Present: %b", v, ok)
  8. Newbie (slices) p := []int{2,3,4,55} i := 2 fmt.Printf("p[%d] ==

    %d\n", i, p[i]) p[1:2] // 3 p[:3] // 2,3,4 p[:2] // 4,55 for i, v := range p { // for _, v := range p fmt.Println("p[%d] = %d", i, v) }
  9. Newbie (function) func main() { plus := func(x, y float64)

    float64 { return x + y } plus(5, 5) } func adder() func(int) int { sum := 0 return func(x int) int { sum += x return sum } }
  10. Newbie (method) // no classes, define method on struct type

    Vertex struct { X, Y float64 } func (v *Vertex) Abs() float64 { return math.Sqrt(v.X*v.X + v.Y*v.Y) } func main() { v := &Vert{3, 4} fmt.Println(v.Abs()) }
  11. Newbie (interface) // interface, a set of methods type Abser

    interface { Abs() float64 } func main() { var a Abser f := Myfloat() a = f } type Myfloat float64 func (f Myfloat) Abs() float64 { }
  12. Newbie (Concurrency) Based on Communicating Sequential Processes (CSP), first described

    in 1978 by C.A.R. Hoare. The Go analogue: goroutines connected by channels Goroutines are like threads, share memory but cheaper!
  13. Newbie (Concurrency) // Goroutines, Channels func sum(a []int, c chan

    int) { sum := 0 for _, v := range a { sum += v } c <- sum } func main() { a := []int{2, 3, 8, 10, 3} c := make(chan int) go sum(a[len(a)/2:], c) }
  14. Newbie (defer, panic, recover) A defer statement pushes a function

    call onto a list. The list of saved calls is executed after the surrounding function returns. Panic is a built-in function that stops the ordinary flow of control and begins panicking. Recover is a built-in function that regains control of a panicking goroutine.
  15. packages archive bufio builtin byptes compress container crypto database debug

    encoding erores expvar flag fmt go hash html image index io log math mime net os path reflect regexp runtime sort strconv strings sync syscall testing text time unicode unsafe