Happy Go Programming Part 1

4,220 views

Published on

Published in: Technology
3 Comments
21 Likes
Statistics
Notes
  • The Go Programming Language (Addison-Wesley Professional Computing Series) --- http://amzn.to/1UI3Avq
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Go in Action --- http://amzn.to/1VpXzUR
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Introducing Go: Build Reliable, Scalable Programs --- http://amzn.to/1T2L4hq
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
4,220
On SlideShare
0
From Embeds
0
Number of Embeds
147
Actions
Shares
0
Downloads
94
Comments
3
Likes
21
Embeds 0
No embeds

No notes for slide

Happy Go Programming Part 1

  1. 1. Happy Go Programming PART I 林佑安 c9s
  2. 2. Intro • was the GitHub Top 1 Contributor ( http://git.io/top ) • 3+ years Perl/VimL programming • 3+ years PHP programming • 2+ years Go programming
  3. 3. Go?
  4. 4. Ken Thompson Rob Pike
  5. 5. Started since 2007
  6. 6. Announced in November 2009
  7. 7. Used in some of Google’s production system
  8. 8. What’s Go • Statically-Typed Language 靜態型別編譯語⾔言 • Built-in Concurrency 內建並發性⽀支持 • Statically-linked Native Binary 靜態連結原⽣生⼆二進位執⾏行檔 • Fast compilation times 極快速的編譯 • Remote package management 內建外部套件管理 • Garbage Collection 使⽤用垃圾收集器 • Safety 安全 (race conditions, type and memory safety for multithreaded program)
  9. 9. About The Language • Concise variable declaration 簡潔的變數定義 • Type Inference 型別推導 • Easy to use 簡易使⽤用 • Composition instead of Inheritance. • Use Interface 使⽤用介⾯面取代 is-a 繼承 • multiple return value 多回傳值函數
  10. 10. About The Tools • 語⾔言規格的修改,可透過 go fix 來將舊版語法或程式碼做⾃自 動修正 • 編碼⾵風格可透過 go fmt 來統⼀一所有格式,以及⾃自動去除空 ⽩白⾏行、換⾏行、縮排等等。 且可以⽤用 grammar 的⽅方式來描述 語法。 • 內建 profiling tools 且⽀支持 Goole pprof ,可直接在 http server 上開 profiler API,利⽤用命令列⼯工具遠端取得 CPU, Memory 使⽤用狀況並進⾏行分析,或產⽣生 call graph 等等資 料。 • 遠端套件可直接透過 go get 安裝
  11. 11. What’s the difference? 那麼有什麼不同呢?
  12. 12. Statically-typed vs Dynamically-typed
  13. 13. Statically-typed vs Dynamically-typed
  14. 14. Statically-typed languages are usually faster
  15. 15. Benchmark • Faster than Node.js, Python, Perl, PHP, Ruby • A bit slower than C, C++ and Java (sometimes faster than Java) • Low memory footprint (10+ times lower than Java)
  16. 16. Benchmark http://www.techempower.com/benchmarks/
  17. 17. and it compiles fast
  18. 18. But it costs a lot of time
  19. 19. Go solves this problem
  20. 20. Concurrency?
  21. 21. Prefork
  22. 22. Prefork
  23. 23. Prefork
  24. 24. Prefork • Hard to share data between processes. • Costs a lot of CPU cycle. • Copying process is time consuming. • Waste a lot of memory.
  25. 25. Just use Go Routine
  26. 26. Just use Go Routine All in one process
  27. 27. Just use Go Routine • Threaded worker pool • use pipeline (channel) to communicate • CSP (Communicating Sequential Processes) http://golang.org/doc/faq
  28. 28. Easier Deployment
  29. 29. Deployment Pain • Install required packages • Install application package dependencies • (dependency hell) • (incompatible version hell) • (10 hours later…) • ok, finally onlined.
  30. 30. You can just build & scp
  31. 31. And it works
  32. 32. Q & A Time
  33. 33. ————————
  34. 34. Ready For Production?
  35. 35. Companies using Go • Google • Carbon Games • Sound Cloud • Iron.io • BCC • SmugMug • Canonical • Bitly • Heroku • CloudFlare
  36. 36. Enough Open Sourced Packages?
  37. 37. 15,298 packages http://godoc.org/-/index
  38. 38. Is it popular?
  39. 39. Supported OS?
  40. 40. Supported OS • Linux • BSD, OpenBSD • Windows • Mac OS • Plan 9
  41. 41. Architectures?
  42. 42. Architectures • i386 • amd64 • arm
  43. 43. Q & A Time
  44. 44. ————————
  45. 45. What You Will Learn Today
  46. 46. Outline • Preparation • Installation / Build Go by yourself. • Hello World Go • Go commands • Go environment • Go packages • Editor and Environment for Go
  47. 47. Outline • Basic Syntax • Type • Built-in Types • Slice • Variable Declaration • Array • Function Declaration • Struct • Function Call • GoRoutine • Package • Channel
  48. 48. Outline • Write something • Your first command-line application. • Your first package. • Build & Install • Using built-in packages. • Using external packages.
  49. 49. Installation • Installation Guide: http://golang.org/doc/install • Go to https://code.google.com/p/go/downloads • Get your installer
  50. 50. Installation: Compile From Source • Install Mercurial: • • port install mercurial • • apt-get install mercurial brew install mercurial Go to http://golang.org/doc/install/source hg clone -u release https://code.google.com/p/go cd go/src ./all.bash
  51. 51. Environment 開發環境
  52. 52. Go IDE • Sublime Text 2 • IntelliJ • LiteIDE • Intype • NetBeans • Eclipse • Zeus http://geekmonkey.org/articles/20-comparison-of-ides-forgoogle-go
  53. 53. go/misc • misc/vim : generic vim plugin • misc/emacs : emacs go mode • misc/git : pre-commit hook (run go fmt before commmit) • misc/bash : bash completion • zsh/go : zsh completion • misc/cgo : cgo examples
  54. 54. vim: gocode • go completion daemon • vim omni completion support • scan code from $GOPATH
  55. 55. Go Environment • $GOROOT ( defaults to /usr/local/go ) • $GOPATH ( your packages ) • $GOARCH • $GOOS
  56. 56. $GOPATH mkdir ~/go export GOPATH=~/go 執⾏行 go get 時,packages 會安裝到 GOPATH 第⼀一個 path 內
  57. 57. $GOPATH mkdir ~/go/vendor mkdir ~/go/private export GOPATH=~/go/vendor:~/go/private 可利⽤用 $GOPATH 將私⽤用 package 分開
  58. 58. $GOPATH mkdir ~/go export GOPATH=~/go path description ~/go/src your source code ~/go/pkg compiled packages (*.a) ~/go/bin command-line binary
  59. 59. Hello World $ vim hello.go package main ! import "fmt" ! func main() { fmt.Printf("hello, worldn") }
  60. 60. Hello World $ go run hello.go $ go build -o hello hello.go $ ./hello
  61. 61. Go commands 編譯後執⾏行程式 (必須是 main package) • go run • go build 編譯 • go install 編譯並且安裝 • go get 抓取遠端套件並且編譯安裝
  62. 62. Basic Syntax
  63. 63. Skeleton File package main ! // main application goes from here package libraryA ! // code goes from here
  64. 64. Built-in Type • int, int8, int16, int32, int64 • uint, uint8, uint16, uint32, uint64 • string • float32, float64
  65. 65. Variable foo := 1 ! var foo = 1 ! var foo int = 1 ! bar := “foo bar” var bar = “foo bar” var bar string = “foo bar”
  66. 66. Variable Scope foo := 1 var foo int = 1 ! ! { { } foo := 2 } var foo int = 2 ! ! // foo 1 // foo = 1
  67. 67. Variable Scope foo := 1 var foo int = 1 ! ! for i := range list { for i := range list { foo := 2 var foo int = 2 } } ! ! // foo 1 // foo = 1
  68. 68. Function Declaration func FunctionName() { // do something }
  69. 69. Function Declaration func FunctionName(x int) int { return x * x }
  70. 70. Function Call ! func main() { x := FunctionName(10) }
  71. 71. Function Call With Package package main ! import “lib1” ! func main() { x := lib1.FunctionName(10) }
  72. 72. Multiple Return Value ! file, err := os.Open(“log.txt”) if err != nil { // handle error }
  73. 73. Writing Your First Command-line Application in Go
  74. 74. Command-line package • package name should be named “main” • the directory name is the compiled binary name. • will be installed to $GOPATH/bin
  75. 75. Writing Your First Go Package
  76. 76. General package • package name should be the same as directory name. • located in $GOPATH/src • will be installed to $GOPATH/pkg
  77. 77. Array items = […]int{ 1, 2, 3 } ! items = [3]int{ 1, 2, 3 }
  78. 78. Slice
  79. 79. Slice items = []int{ 1, 2, 3 }
  80. 80. Slice: Appending item items = []int{ 1, 2, 3 } ! items = append(items, 4)
  81. 81. Slice: Extracting items = []int{ 1, 2, 3 } ! copiedItems = items[:] subItems = items[1:2]
  82. 82. Array vs Slice • Arrays are fixed-length. Slices are not. • Arrays are faster than slices.
  83. 83. Struct
  84. 84. Struct • Exported fields starts with upper case letter. • Private fields starts with lower case letter. • Code in the same package can access private fields and private functions. • External package can not access private fields and private functions.
  85. 85. Interface
  86. 86. Type Checking
  87. 87. Type Switch
  88. 88. Universal Interface var x = interface{} ! func Convert(a interface{}) { // do the conversion
 } ! Convert(Foo{}) Convert(Bar{}) Convert(&Foo{}) Convert(&Bar{})
  89. 89. Thank you
  90. 90. • Contact: • Twitter: @c9s • Plurk: @c9s • GitHub: @c9s • Facebook: http://facebook.com/yoan.lin • yoanlin93@gmail.com

×