introduction to golang final

141
一天學會 Go 語言 2017.03.18 吳柏毅 Bo-Yi Wu

Upload: paul-chao

Post on 07-Apr-2017

97 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Introduction to Golang final

一天學會 Go 語言 2017.03.18

吳柏毅 Bo-Yi Wu

Page 2: Introduction to Golang final

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

GitHub: http://github.com/appleboy

  Slide: https://www.slideshare.net/appleboy

Page 3: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 4: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 5: Introduction to Golang final

Go 基本簡介

 Go 語言誕生

 Go 語言優勢

 Go 語言選擇

 誰在用 Go 語言

Page 6: Introduction to Golang final

Robert Griesemer, Rob Pike 和 Ken Thompson

Page 7: Introduction to Golang final

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

Page 8: Introduction to Golang final

為什麼設計 Go 語言

Page 9: Introduction to Golang final

Google 遇到的問題

  大量的 C++ 代码,同時引入 Java 和 Python

  成千上萬的工程师

  數百萬的程式碼

  分散式編譯系統

  數百萬的伺服器

Page 10: Introduction to Golang final

Google 痛點

  編譯非常慢

  沒用的相依性檔案

  工程師只用了一個語言一小部分

  程式碼難以維護 (可讀性差,⽂文件不清楚)

  部署時間越來越長

  交叉編譯困難

Page 11: Introduction to Golang final

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

  強制類型

  Function 和 Method

  没有錯誤處理

  ⽤用字⾸首來區別可否存取

  不⽤用的 Import 或變數會引起編譯錯誤

  完整的標準函式

  支援 UTF-8 格式

Page 12: Introduction to Golang final

Go at Google: Language Design in the Service of Software Engineering

https://talks.golang.org/2012/splash.article

Page 13: Introduction to Golang final

為什麼要導入 Go 語言 Team work

Page 14: Introduction to Golang final

Go 帶給團隊優勢

 學習曲線

 開發及執行效率

 由 Google 維護

 部署方便

 跨平台編譯

 內建 Coding Style, Testing 等工具

 多核心處理

Page 15: Introduction to Golang final

誰在用 Go 語言

Page 16: Introduction to Golang final
Page 17: Introduction to Golang final

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

Page 18: Introduction to Golang final
Page 19: Introduction to Golang final
Page 20: Introduction to Golang final
Page 21: Introduction to Golang final
Page 22: Introduction to Golang final
Page 23: Introduction to Golang final
Page 24: Introduction to Golang final
Page 25: Introduction to Golang final
Page 26: Introduction to Golang final
Page 27: Introduction to Golang final
Page 28: Introduction to Golang final
Page 29: Introduction to Golang final
Page 30: Introduction to Golang final

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

Page 31: Introduction to Golang final

Switched from other languages. PHP, Python, Node.js, Java, C++

https://github.com/golang/go/wiki/FromXToGo

Page 32: Introduction to Golang final

如何將 Go 語言導入團隊

Page 33: Introduction to Golang final

  學習曲線   程式碼簡潔

  沒有物件導向

  團隊開發工具整合   Coding Style

  Testing Tool

  Benchmark Tool

  性能分析

  部署環境   降低部署時間

  降低測試時間

  重啟時間非常快,Load-Balancer 不需要 Pre-warning

  系統效能 (記憶體用量, CPU 使用率 …)   EC2 使用量降低

Page 34: Introduction to Golang final

A push notification server written in Go

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

Page 35: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 36: Introduction to Golang final

安裝 Go 環境 請直接安裝 Go Version Manager

https://github.com/moovweb/gvm

Page 37: Introduction to Golang final

安裝步驟

gvm install go1.4 -B

gvm use go1.4

 export GOROOT_BOOTSTRAP=$GOROOT

gvm install go1.8

Page 38: Introduction to Golang final

請在終端機執行 go env

Page 39: Introduction to Golang final
Page 40: Introduction to Golang final

修改 GOPATH 路徑

export GOPATH=/Users/appleboy/git/go 注意: Go 1.8 之後已經預設將 GOPATH 設定在 ${HOME}/go 路徑

Page 41: Introduction to Golang final

GOPATH ⺫⽬目錄局結構

 bin  編譯後產生可執行檔案

src  存放程式碼

pkg  放置編譯後的 .a 檔案

Page 42: Introduction to Golang final
Page 43: Introduction to Golang final

Go 指令介紹

  go get

  go run

  go test

  go fmt

  go install

  go build

  go doc

Page 44: Introduction to Golang final

在本機端看 Go 文件 godoc –http=:8088

Page 45: Introduction to Golang final

Go 程式碼品質

Page 46: Introduction to Golang final

gofmt https://blog.golang.org/go-fmt-your-code

Page 47: Introduction to Golang final

gofmt

 程式碼自動化排版

 整合各大編輯器 (Atom, Sublime, VS code, Vim .. 等)

 重構程式碼

Page 48: Introduction to Golang final

golint https://github.com/golang/lint

Page 49: Introduction to Golang final

golint

  變數命名方式

  確保文件撰寫

Page 50: Introduction to Golang final

Go 編輯器 Visual Studio Code

https://code.visualstudio.com

Page 51: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go 套件製作

  Go concurrency 介紹

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 52: Introduction to Golang final
Page 53: Introduction to Golang final

Import 可以使用別名 可以省略引用

Page 54: Introduction to Golang final

Import 順序

 內建 Package

 自建 Package

 外部 Package

Page 55: Introduction to Golang final
Page 56: Introduction to Golang final

Package 用大小寫決定

使用權限

Page 57: Introduction to Golang final

大小寫區別 Private 及 Public

Page 58: Introduction to Golang final

變數宣告 const, var, :=

Page 59: Introduction to Golang final

變數宣告

  使用 const

  使用 := (常用)

  使用 var

Page 60: Introduction to Golang final

const 使用 iota

Page 61: Introduction to Golang final

if 條件子句

Page 62: Introduction to Golang final

if 條件子句

  右括號必須跟 if 或 else 同一行

  支援私有變數,只在 if 內使用

Page 63: Introduction to Golang final

switch

  不需要寫 break

fallthrough 可以繼續執⾏行下⼀一個 case

Page 64: Introduction to Golang final

Array, Slice

Page 65: Introduction to Golang final

Functions

Page 66: Introduction to Golang final

function

  Return

  Return naming

  Return multiple

Page 67: Introduction to Golang final

作業

  請寫出一個 func 加總所有輸入值及平均值 Func(4,6) = 10, 5

Func(4,6,10) = 20, 6.66

  請忽略負數值 Func(4, -2, 100) = 104, 34

Page 68: Introduction to Golang final

Variadic Functions Go func 不提供預設值

Page 69: Introduction to Golang final

Structs, Methods

Page 70: Introduction to Golang final

作業

  實作計算機功能   初始值 New(int)

  加法 func Add(input …int)

  減法 func Sub(input …int)

  輸出

Page 71: Introduction to Golang final

Interface

Page 72: Introduction to Golang final

作業

  實作兩台計算機功能   初始值(一台為 100, 一台為 200)   加法(一台 * 2, 一台 * 3)   減法(一台 * 2, 一台 * 4)   輸出

  Add(100,200,300)

  Sub(50, 60)

  計算機 1: 輸出為 1080

  計算機 2: 輸出為 1560

Page 73: Introduction to Golang final

Type assertion and type switch Interface

Page 74: Introduction to Golang final

作業

  請用單一 func 實作底下功能   輸入數字 100 轉換成字串 100

  輸入字串 100 轉換成數字 100

Page 75: Introduction to Golang final

init function

Page 76: Introduction to Golang final

錯誤處理 Error Handling

https://golang.org/pkg/errors/

Page 77: Introduction to Golang final

錯誤處理

  errors.New

  fmt.Errorf

Page 78: Introduction to Golang final

自訂錯誤 Error() string

https://blog.golang.org/error-handling-and-go

Page 79: Introduction to Golang final

作業

  撰寫底下 func 功能   輸入數字小於 10 則回傳錯誤

  自訂錯誤輸入訊息   您的輸入 %d 小於 10

Page 80: Introduction to Golang final

Vendor 管理

Page 81: Introduction to Golang final

請直接安裝 Package Management Tools

https://github.com/golang/go/wiki/PackageManagementTools

Page 82: Introduction to Golang final

推薦套件 govendor https://github.com/kardianos/govendor

Page 83: Introduction to Golang final

Go 也想統一 dependency tool https://github.com/golang/dep

Page 84: Introduction to Golang final

寫測試 Go 內建測試 Tool

Page 85: Introduction to Golang final

測試完整 package go test -v -cover -coverprofile=coverage.txt ./…

Page 86: Introduction to Golang final

測試單一函數 go test -v -cover -run=TestHelloWorld ./example/…

Page 87: Introduction to Golang final

測試覆蓋率報表 go tool cover -html=coverage.txt

Page 88: Introduction to Golang final
Page 89: Introduction to Golang final

作業

  請將前面作業補上基本測試,覆蓋率達 80 %

Page 90: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 91: Introduction to Golang final

Go supports concurrency

  concurrent execution (goroutines)

  synchronization and messaging (channels)

  multi-way concurrent control (select)

Page 92: Introduction to Golang final

Concurrency vs. Parallelism https://blog.golang.org/concurrency-is-not-parallelism

Page 93: Introduction to Golang final

Channel Blocking vs Non-Blocking

Page 94: Introduction to Golang final

sync.WaitGroup

Page 95: Introduction to Golang final

Select

Page 96: Introduction to Golang final

作業

  建立兩個 Channel errChannel := make(chan error, 1)

  finished := make(chan bool, 1)

  用 For 跑 10 個 Job   其中第五個 Job 回傳 err chan 後結束程式

  跑完 10 個 Job 則回傳 finished chan 後結束程式   用 sync.WaitGroup 實現

Page 97: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go Concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 98: Introduction to Golang final

Web Clients https://golang.org/pkg/net/http#Client

https://golang.org/pkg/net/http#Request https://golang.org/pkg/net/http#Response

Page 99: Introduction to Golang final

Web Servers http.ListenAndServe

Page 100: Introduction to Golang final

HandleFunc

Page 101: Introduction to Golang final

NewServeMux & ServeHTTP

Page 102: Introduction to Golang final

自訂 Http.Server

Page 103: Introduction to Golang final

作業

  撰寫 Http Server Listen on 8088 port   /hello 輸出 Hello Golang

Page 104: Introduction to Golang final

Go 1.8 Closed Server os.Signal

Page 105: Introduction to Golang final

選擇 Web Framework https://awesome-go.com/#web-frameworks

Page 106: Introduction to Golang final

Gin, Beego, Echo …

Page 107: Introduction to Golang final
Page 108: Introduction to Golang final
Page 109: Introduction to Golang final

豐富的 Middleware https://github.com/gin-contrib

Page 110: Introduction to Golang final

快速寫 Web Testing https://github.com/appleboy/gofight

https://awesome-go.com/#testing

Page 111: Introduction to Golang final
Page 112: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 113: Introduction to Golang final

flag package https://golang.org/pkg/flag

Page 114: Introduction to Golang final

flag package 不支援環境變數 os.Getenv()

Page 115: Introduction to Golang final

urfave/cli https://github.com/urfave/cli

https://blog.wu-boy.com/2017/02/write-command-line-in-golang/

Page 116: Introduction to Golang final

https://github.com/spf13/cobra

Page 117: Introduction to Golang final

跨平台編譯 GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o hello package GOOS=linux GOARCH=arm CGO_ENABLED=0 go build -o hello package

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

Page 118: Introduction to Golang final

Go cross compile tool https://github.com/mitchellh/gox

Page 119: Introduction to Golang final
Page 120: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 121: Introduction to Golang final
Page 122: Introduction to Golang final

Docker Image docker build -t example-alpine -f Dockerfile.golang .

Page 123: Introduction to Golang final
Page 124: Introduction to Golang final

Alpine Linux 挑戰最小 docker image OS

https://goo.gl/bLd9FA

Page 125: Introduction to Golang final

golang:1.8.0-alpine

Page 126: Introduction to Golang final

更小 scratch

Page 127: Introduction to Golang final
Page 128: Introduction to Golang final

x509: failed to load system roots and no roots provided

Page 129: Introduction to Golang final

centurylink/ca-certs

Page 130: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Page 131: Introduction to Golang final

Downsize go binary? Go 1.5

Page 132: Introduction to Golang final

Using shared library in Go this feature is available on linux/amd64 platform

Page 133: Introduction to Golang final

Compile standard builtin packages go install -buildmode=shared -linkshared std

Page 134: Introduction to Golang final

Compile your share library go install -buildmode=shared –linkshared calc

Page 135: Introduction to Golang final

Using shared Go library in C

Page 136: Introduction to Golang final

Go functions can be executed from C applications.

//export <your_function_name> go build -buildmode=c-shared –o hello.so hello.go

Page 137: Introduction to Golang final

buildmode=plugin go build -buildmode=plugin -o hello.so hello.go

Go 1.8 feature

Page 138: Introduction to Golang final

Using C Library in Go

Page 139: Introduction to Golang final

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人 (作業)

Page 140: Introduction to Golang final

Bot 機器人

  Slack

  Facebook

  Line

  Telegram

Page 141: Introduction to Golang final

作業

  整合 Go Cli Tool:   ./main server 用來接受 fb 或 line callback

  ./main send 用來傳送訊息給單一使用者   ./main send -u user -m message