Go lang introduction

2,568 views

Published on

go lang

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,568
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
40
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Go lang introduction

  1. 1. Go Lang Introduction yangwm http://t.sina.com.cn/yangwm
  2. 2. A response to Google’s internal needs: - efficient large scale programming - speed of compilation - distributed systems - multicore hardware - networked computing
  3. 3. GO: New Experimental Concurrent Garbage Collected Systems Language
  4. 4. A complete (if simple) web server: import ( "fmt" "http" ) func handler(c *http.Conn, r *http.Request) { fmt.Fprintf(c, "Hello, %s.n", r.URL.Path[1:]) } func main() { http.ListenAndServe(":8080", http.HandlerFunc(handler)) }
  5. 5. concepts: * Go's concurrency is strongly influenced by Communicating Sequential Processes (Hoare, 1978) * Go has types and interfaces, but no inheritance. It is arguably more object-oriented than previously mentioned languages, being closer to the original Smalltalk meaning (1970s)
  6. 6. interfaces: type SortInterface interface { Len() int Less(i, j int) bool Swap(i, j int) }
  7. 7. Concurrent: * In traditional concurrent programs, you communicate by sharing memory. In Go, you share memory by communicating. * Threads and locks are concurrency primitives; CSP is a concurrency model.
  8. 8. Parallel computation is easy: var a string func f() { a = "hello, world" } func main() { go f() println(a) }
  9. 9. via lock: var l sync.Mutex var a string func f() { a = "hello, world" l.Unlock() } func main() { l.Lock() go f() l.Lock() println(a) }
  10. 10. via channel: var c = make(chan int, 10) var a string func f() { a = &quot;hello, world&quot; c <- 0 } func main() { go f() <- c println(a) }
  11. 11. via once: var a string var once sync.Once func setup() { println(&quot;invoke setup()&quot;) a = &quot;hello, world&quot; } func doprint() { once.Do(setup) print(a) } func twoprint() { go doprint() go doprint() }
  12. 12. thank you

×