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 full-size 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 full-size slide

  3. 2017

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

    View full-size slide

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

    View full-size slide

  5. Robert Griesemer, Rob Pike 和 Ken Thompson

    View full-size slide

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

    View full-size slide

  7. 1. Ian Lance Taylor 加入

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  11. Golang in China

    View full-size slide

  12. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

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

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

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

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

    View full-size slide

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

    強制類型
    Function 和 Metho
    d

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

    View full-size slide

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

    View full-size slide

  18. 強制類型

    View full-size slide

  19. Function 和 Method

    View full-size slide

  20. 没有錯誤處理

    View full-size slide

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

    View full-size slide

  22. Import 錯誤

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  25. 誰在⽤ Go 語⾔

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  28. Switched from other languages.

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

    View full-size slide

  29. 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 full-size slide

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

    View full-size slide

  31. Farewell Node.js
    T
    J

    https://goo.gl/WVxwtb
    44

    View full-size slide

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

    View full-size slide

  33. 學習曲線
    程式碼簡潔
    沒有物件導向
    團隊開發⼯具整合
    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 full-size slide

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

    View full-size slide

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

    View full-size slide

  36. 實際案例
    A simple notification servic
    e

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

    View full-size slide

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

    View full-size slide

  38. $ 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 full-size slide

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

    View full-size slide

  40. 跨平台編譯
    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 full-size slide

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


    View full-size slide

  42. Go 核⼼功能
    goroutine 和 channel

    View full-size slide

  43. Do not communicate by
    sharing memory;

    instead, share memory by
    communicating.

    View full-size slide

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

    View full-size slide

  45. MogoDB Transaction
    Perform Two Phase Commits.

    View full-size slide

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

    View full-size slide

  47. 使⽤ Lock 及 Unlock 解決

    View full-size slide

  48. 效能分析 (使⽤ Lock)

    View full-size slide

  49. 使⽤ Channel 解決
    goroutine 和 channel

    View full-size slide

  50. 初始化 Channel

    View full-size slide

  51. 效能分析 (使⽤ Channel)

    View full-size slide

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

    View full-size slide

  53. 多個 Go Application
    optimistic concurrency

    View full-size slide

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

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

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

    View full-size slide

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

    View full-size slide