Slide 1

Slide 1 text

Go 語⾔簡介 吳柏毅 Bo-Yi W u https://blog.wu-boy.com/

Slide 2

Slide 2 text

Mediatek Engineer DevOps Golang, PHP, Node.js .. Some open source Gitea Gin Drone appleboy @GitHub appleboy @twitter appleboy @slideshare appleboy46 @facebook About Me 2

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Robert Griesemer, Rob Pike 和 Ken Thompson

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

1. Ian Lance Taylor 加入

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Golang in China

Slide 13

Slide 13 text

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 (最新版)

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Google 遇到的問題 ⼤量的 C++ 代码,同時引入 Java 和 Pytho n 成千上萬的⼯程师 (每個⼈風格不同 ) 數百萬的程式碼 (如何減少代碼產量 ) 分散式編譯系統 (交叉編譯速度 … ) 數百萬的伺服器 (部署時間 …)

Slide 17

Slide 17 text

Go 語⾔特性 沒有物件導向 (無繼承特性 ) 強制類型 Function 和 Metho d 没有錯誤處理 ⽤字⾸來區別可否存取 不⽤的 Import 或變數會引起編譯錯誤 完整的標準函式 ⽀援 UTF-8 格式

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

強制類型

Slide 20

Slide 20 text

Function 和 Method

Slide 21

Slide 21 text

没有錯誤處理

Slide 22

Slide 22 text

⽤字⾸來區別可否存取

Slide 23

Slide 23 text

Import 錯誤

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

誰在⽤ Go 語⾔

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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/

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

Farewell Node.js T J https://goo.gl/WVxwtb 44

Slide 45

Slide 45 text

如何將 Go 語⾔導入團隊

Slide 46

Slide 46 text

學習曲線 程式碼簡潔 沒有物件導向 團隊開發⼯具整合 Coding Style Testing Tool Benchmark Too l 部署環境 (Go 1.5 Cross Compiler ) 降低部署時間 降低測試時間 重啟時間非常快,Load-Balancer 不需要 Pre-warning 系統效能 (記憶體⽤量, CPU 使⽤率 … ) EC2 使⽤量降低 (降低 80 ~ 85%) Response time 100ms -> 10ms

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

實際案例 A simple notification servic e https://github.com/appleboy/gorush/

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

$ for i in {1..9999999}; do bat -b.N=1000 -b.C=100 POST localhost:8088/api/push notifications:[email protected]; sleep 1; done

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

跨平台編譯 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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Go 核⼼功能 goroutine 和 channel

Slide 57

Slide 57 text

Do not communicate by sharing memory; 
 instead, share memory by communicating.

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

MogoDB Transaction Perform Two Phase Commits.

Slide 60

Slide 60 text

main func

Slide 61

Slide 61 text

Handle func

Slide 62

Slide 62 text

使⽤ sync.Mutex 解決 Lock and Unlock

Slide 63

Slide 63 text

使⽤ Lock 及 Unlock 解決

Slide 64

Slide 64 text

效能分析 (使⽤ Lock)

Slide 65

Slide 65 text

使⽤ Channel 解決 goroutine 和 channel

Slide 66

Slide 66 text

初始化 Channel

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

效能分析 (使⽤ Channel)

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

多個 Go Application optimistic concurrency

Slide 72

Slide 72 text

optimistic concurrency 使⽤ sync.Mutex 解決問題? (X ) 使⽤ goroutine + Channel 設計單⼀ Queue? (X ) 使⽤ goroutine + Channel 設計多重 Queue? (X)

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

請發問?