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

Go 語言基礎簡介

Go 語言基礎簡介

Go 語言入門介紹,底下是大綱:

* Go 語言誕生
* Go 語言優勢
* Go 語言選擇
* 誰在用 Go 語言

最後會提到 goroutine 及 channel 這兩大特性。如果對 Go 語言有興趣,可以參考線上課程: https://blog.wu-boy.com/golang-online-course/

Bo-Yi Wu

March 27, 2018
Tweet

More Decks by Bo-Yi Wu

Other Decks in Programming

Transcript

  1. Go 語⾔簡介
    吳柏毅 Bo-Yi W
    u

    https://blog.wu-boy.com/

    View Slide

  2. Mediatek Engineer


    DevOps


    Golang, PHP, Node.js ..


    Some open source


    Gitea


    Gin


    Drone


    appleboy @GitHub


    appleboy @twitter


    appleboy @slideshare


    appleboy46 @facebook
    About Me
    2

    View Slide

  3. 2017

    Google Survey Result
    https://blog.golang.org/survey2017-results

    View Slide

  4. View Slide

  5. Go 基本簡介
    Go 語⾔誕⽣
    Go 語⾔優勢
    Go 語⾔選擇
    誰在⽤ Go 語⾔

    View Slide

  6. Robert Griesemer, Rob Pike 和 Ken Thompson

    View Slide

  7. Go 語⾔初期發展
    四件事情讓 Go 發展得更好

    View Slide

  8. 1. Ian Lance Taylor 加入

    View Slide

  9. 2. Russ Cox 在 2008 年加入
    實現了 http.HandlerFunc 及 io 接⼝

    View Slide

  10. 3. 安全專家 Adam Langley
    golang.org 網站及 build dashboard

    View Slide

  11. 4. Docker 及 Kubernets 使⽤ GO
    2013 年及 2014 年

    View Slide

  12. Golang in China

    View Slide

  13. Go 發佈週期 (半年⼀版)
    2013/05 1.
    1

    2013/12 1.
    2

    2014/06 1.
    3

    2014/12 1.
    4

    2015/08 1.5 (Google 規定以後每半年發佈⼀版
    )

    2016/02 1.
    6

    2016/08 1.
    7

    2017/02 1.
    8

    2017/08 1.
    9

    2018/02 1.10 (最新版)

    View Slide

  14. Go 語⾔發展歷史(漫畫版)
    https://goo.gl/jGcwXK

    View Slide

  15. 為什麼設計 Go 語⾔
    根據 Rob Pike ⼤神描述 …

    View Slide

  16. Google 遇到的問題
    ⼤量的 C++ 代码,同時引入 Java 和 Pytho
    n

    成千上萬的⼯程师 (每個⼈風格不同
    )

    數百萬的程式碼 (如何減少代碼產量
    )

    分散式編譯系統 (交叉編譯速度 …
    )

    數百萬的伺服器 (部署時間 …)

    View Slide

  17. Go 語⾔特性
    沒有物件導向 (無繼承特性
    )

    強制類型
    Function 和 Metho
    d

    没有錯誤處理
    ⽤字⾸來區別可否存取
    不⽤的 Import 或變數會引起編譯錯誤
    完整的標準函式
    ⽀援 UTF-8 格式

    View Slide

  18. 沒有物件導向 (無繼承特性)

    View Slide

  19. 強制類型

    View Slide

  20. Function 和 Method

    View Slide

  21. 没有錯誤處理

    View Slide

  22. ⽤字⾸來區別可否存取

    View Slide

  23. Import 錯誤

    View Slide

  24. Go at Google: Language Design
    in the Service of Software
    Engineering
    https://talks.golang.org/2012/splash.article

    View Slide

  25. Go 優勢
    學習曲線
    開發及執⾏效率
    由 Google 維護
    部署⽅便
    跨平台編譯
    內建 Coding Style, Testing 等⼯具
    多核⼼處理

    View Slide

  26. 誰在⽤ Go 語⾔

    View Slide

  27. View Slide

  28. Go ⼤型專案
    https://github.com/golang/go/wiki/Projects

    View Slide

  29. View Slide

  30. View Slide

  31. View Slide

  32. View Slide

  33. View Slide

  34. View Slide

  35. View Slide

  36. View Slide

  37. View Slide

  38. View Slide

  39. View Slide

  40. Why Go
    https://github.com/golang/go/wiki/whygo

    View Slide

  41. Switched from other languages.

    PHP, Python, Node.js, Java, C++
    https://github.com/golang/go/wiki/FromXToGo

    View Slide

  42. 5 Reasons Why We switched from Python To Go
    https://hackernoon.com/5-reasons-why-we-switched-from-python-to-go-4414d5f42690
    中⽂: https://blog.wu-boy.com/2017/04/5-reasons-why-we-switched-from-python-to-go/

    View Slide

  43. From Python to Go: migrating our entire API
    https://blog.repustate.com/migrating-entire-api-go-python/

    View Slide

  44. Farewell Node.js
    T
    J

    https://goo.gl/WVxwtb
    44

    View Slide

  45. 如何將 Go 語⾔導入團隊

    View Slide

  46. 學習曲線
    程式碼簡潔
    沒有物件導向
    團隊開發⼯具整合
    Coding Style
    Testing Tool
    Benchmark Too
    l

    部署環境 (Go 1.5 Cross Compiler
    )

    降低部署時間
    降低測試時間
    重啟時間非常快,Load-Balancer 不需要 Pre-warning
    系統效能 (記憶體⽤量, CPU 使⽤率 …
    )

    EC2 使⽤量降低 (降低 80 ~ 85%)
    Response time 100ms -> 10ms

    View Slide

  47. 47
    https://talks.golang.org/2014/gocon-tokyo.slide

    View Slide

  48. 48
    https://talks.golang.org/2014/gocon-tokyo.slide

    View Slide

  49. 實際案例
    A simple notification servic
    e

    https://github.com/appleboy/gorush/

    View Slide

  50. A push notification server written in Go
    https://github.com/appleboy/gorush/

    View Slide

  51. $ for i in {1..9999999}; do bat -b.N=1000 -b.C=100 POST localhost:8088/api/push


    notifications:[email protected]; sleep 1; done

    View Slide

  52. 從商業利益看 Go 程式語⾔
    https://blog.wu-boy.com/2017/01/business-benefits-of-go/

    View Slide

  53. 跨平台編譯
    GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o hello packag
    e

    GOOS=linux GOARCH=arm CGO_ENABLED=0 go build -o hello packag
    e

    GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o hello packag
    e

    GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o hello.exe package

    View Slide

  54. Portable
    54
    https://github.com/mitchellh/gox


    View Slide

  55. View Slide

  56. Go 核⼼功能
    goroutine 和 channel

    View Slide

  57. Do not communicate by
    sharing memory;

    instead, share memory by
    communicating.

    View Slide

  58. 如何設計單⼀ Queue
    解決同時寫入 DB 問題 (Transaction)

    View Slide

  59. MogoDB Transaction
    Perform Two Phase Commits.

    View Slide

  60. main func

    View Slide

  61. Handle func

    View Slide

  62. 使⽤ sync.Mutex 解決
    Lock and Unlock

    View Slide

  63. 使⽤ Lock 及 Unlock 解決

    View Slide

  64. 效能分析 (使⽤ Lock)

    View Slide

  65. 使⽤ Channel 解決
    goroutine 和 channel

    View Slide

  66. 初始化 Channel

    View Slide

  67. View Slide

  68. View Slide

  69. 效能分析 (使⽤ Channel)

    View Slide

  70. 如何設計多個 Queue
    解決單⼀ Queue 效能問題

    View Slide

  71. 多個 Go Application
    optimistic concurrency

    View Slide

  72. optimistic concurrency
    使⽤ sync.Mutex 解決問題? (X
    )

    使⽤ goroutine + Channel 設計單⼀ Queue? (X
    )

    使⽤ goroutine + Channel 設計多重 Queue? (X)

    View Slide

  73. Udemy 線上課程
    https://www.udemy.com/golang-fight/?couponCode=GOLANG-INTRO

    View Slide

  74. 請發問?

    View Slide