Introduction to Golang final

Preview:

Citation preview

一天學會 Go 語言 2017.03.18

吳柏毅 Bo-Yi Wu

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

GitHub: http://github.com/appleboy

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

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Go 基本簡介

 Go 語言誕生

 Go 語言優勢

 Go 語言選擇

 誰在用 Go 語言

Robert Griesemer, Rob Pike 和 Ken Thompson

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

為什麼設計 Go 語言

Google 遇到的問題

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

  成千上萬的工程师

  數百萬的程式碼

  分散式編譯系統

  數百萬的伺服器

Google 痛點

  編譯非常慢

  沒用的相依性檔案

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

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

  部署時間越來越長

  交叉編譯困難

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

  強制類型

  Function 和 Method

  没有錯誤處理

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

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

  完整的標準函式

  支援 UTF-8 格式

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

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

為什麼要導入 Go 語言 Team work

Go 帶給團隊優勢

 學習曲線

 開發及執行效率

 由 Google 維護

 部署方便

 跨平台編譯

 內建 Coding Style, Testing 等工具

 多核心處理

誰在用 Go 語言

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

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

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

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

如何將 Go 語言導入團隊

  學習曲線   程式碼簡潔

  沒有物件導向

  團隊開發工具整合   Coding Style

  Testing Tool

  Benchmark Tool

  性能分析

  部署環境   降低部署時間

  降低測試時間

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

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

A push notification server written in Go

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

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

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

https://github.com/moovweb/gvm

安裝步驟

gvm install go1.4 -B

gvm use go1.4

 export GOROOT_BOOTSTRAP=$GOROOT

gvm install go1.8

請在終端機執行 go env

修改 GOPATH 路徑

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

GOPATH ⺫⽬目錄局結構

 bin  編譯後產生可執行檔案

src  存放程式碼

pkg  放置編譯後的 .a 檔案

Go 指令介紹

  go get

  go run

  go test

  go fmt

  go install

  go build

  go doc

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

Go 程式碼品質

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

gofmt

 程式碼自動化排版

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

 重構程式碼

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

golint

  變數命名方式

  確保文件撰寫

Go 編輯器 Visual Studio Code

https://code.visualstudio.com

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go 套件製作

  Go concurrency 介紹

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

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

Import 順序

 內建 Package

 自建 Package

 外部 Package

Package 用大小寫決定

使用權限

大小寫區別 Private 及 Public

變數宣告 const, var, :=

變數宣告

  使用 const

  使用 := (常用)

  使用 var

const 使用 iota

if 條件子句

if 條件子句

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

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

switch

  不需要寫 break

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

Array, Slice

Functions

function

  Return

  Return naming

  Return multiple

作業

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

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

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

Variadic Functions Go func 不提供預設值

Structs, Methods

作業

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

  加法 func Add(input …int)

  減法 func Sub(input …int)

  輸出

Interface

作業

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

  Add(100,200,300)

  Sub(50, 60)

  計算機 1: 輸出為 1080

  計算機 2: 輸出為 1560

Type assertion and type switch Interface

作業

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

  輸入字串 100 轉換成數字 100

init function

錯誤處理 Error Handling

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

錯誤處理

  errors.New

  fmt.Errorf

自訂錯誤 Error() string

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

作業

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

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

Vendor 管理

請直接安裝 Package Management Tools

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

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

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

寫測試 Go 內建測試 Tool

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

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

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

作業

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

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Go supports concurrency

  concurrent execution (goroutines)

  synchronization and messaging (channels)

  multi-way concurrent control (select)

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

Channel Blocking vs Non-Blocking

sync.WaitGroup

Select

作業

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

  finished := make(chan bool, 1)

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

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

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go Concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

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

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

Web Servers http.ListenAndServe

HandleFunc

NewServeMux & ServeHTTP

自訂 Http.Server

作業

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

Go 1.8 Closed Server os.Signal

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

Gin, Beego, Echo …

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

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

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

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

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

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

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

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

https://github.com/spf13/cobra

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

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

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

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

Alpine Linux 挑戰最小 docker image OS

https://goo.gl/bLd9FA

golang:1.8.0-alpine

更小 scratch

x509: failed to load system roots and no roots provided

centurylink/ca-certs

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

  Go 實作 Line, Facebook 機器人

Downsize go binary? Go 1.5

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

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

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

Using shared Go library in C

Go functions can be executed from C applications.

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

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

Go 1.8 feature

Using C Library in Go

課程大綱   Go 基本簡介

  Go 環境建置

  Go 基本語法

  Go concurrency 介紹

  Go 簡易 HTTP 伺服器

  Go 簡易 Command Line 實作

  Go 搭配 Docker 介紹

  Go 結合 C 語⾔言

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

Bot 機器人

  Slack

  Facebook

  Line

  Telegram

作業

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

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