- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
柴树杉-Go语言将走向何方
展开查看详情
1 .2018 - Go ? (chai2010) @ QingCloud 1
2 .第七届 全球软件案例研究峰会 AI/ AIOps DevOps AI 2018 11 30 -12 3 | 100+
3 . 上海精品公开课 管理3.0认证课程 时间:12月22-23日 | 地点:上海 | 讲师:林伟丹 大数据及AI挖掘技术 时间:12月22-23日 | 地点:上海 | 讲师:风清扬 高可用架构与设计 时间:01月12-13日 | 地点:上海 | 讲师:沈老师 K8S与service mesh 时间:01月12-13日 | 地点:上海 | 讲师:Jim 备注:扫码查看课程详情,两人以上报名有优惠,详情咨询:15802217295
4 . (chai2010) •Go , Gopher •<Go ><WebAssembly > • QingCloud OpenPitrix •https://github.com/chai2010 • ! 4 • Less is more!
5 .5
6 .2020 Go2 2030 Go1 6
7 .1 (Go1.11+) 7
8 .1.1 8
9 .1.2 - 01 $ export GO111MODULE=on $ mkdir myapp // myapp/hello.go package main import "fmt" func main() { 9
10 .1.2 - 02 // myapp/go.mod module myapp $ go run hello.Go hello module go.mod 10
11 .1.3 go.mod go.sum module rsc.io/quote require ( rsc.io/quote/v3 v3.0.0 golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c rsc.io/sampler v1.3.0 h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8= ) golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= rsc.io/quote/v2 v2.0.1 h1:DF8hmGbDhgiIa2tpqLjHLIKkJx6WjCtLEqZBAU+hACI= 11 rsc.io/quote/v2 v2.0.1/go.mod h1:EgjyEkPoRlzZbvGiUV/6yo8qd6yeDd/CP/9lRtfg4PU=
12 .1.4 go.mod module my/thing require ( other/thing v1.0.2 new/thing v2.3.4 ) exclude ( replace old/thing v1.2.3 12
13 .1.5 go get go get -u # to use the latest minor or patch releases go get -u=patch # to use the latest patch releases go get github.com/gorilla/mux@master # records current meaning of master go get github.com/gorilla/mux@latest # same (@latest is default) go get github.com/gorilla/mux@v1.6.2 # records v1.6.2 go get github.com/gorilla/mux@'<v1.6.2' # records < v1.6.2 go get github.com/gorilla/mux@'>v1.6.2' # records > v1.6.2 go get github.com/gorilla/mux@e3702bed2 # records v1.6.2 go get github.com/gorilla/mux@c856192 # records v0.0.0-20180517173623-c856.. 13
14 .1.6 v(major).(minor).(patch), such as v0.1.0, v1.2.3, or v3.0.1 major - API minor - API pahch - API BUG v0.x.y - API v1.x.y - v1 v2.x.y - v1 v2 14
15 .1.7 v1/v2/v3 import "github.com/my/module/pkg/foo" // v0 or v1 import "github.com/my/module/v2/pkg/foo" // v2 import "github.com/my/module/v3/pkg/foo" // v3 v0/v1 go.mod v0/v v0.x.y v1.x.y tag v2 v2 15
16 .1.8 v2/v3 import "github.com/my/module/pkg/foo" // v0 or v1 import "github.com/my/module/submod/pkg" // submod v0/v1 go.mod submod v2/v3 submod go.mod submod submod/v1.2.3 tag 16
17 .1.9 17
18 .1.10 require ( github.com/coreos/etcd v3.3.10+incompatible github.com/golang/protobuf v1.2.0 golang.org/x/net v0.0.0-20181029044818-c440 // indirect golang.org/x/sync v0.0.0-20180314180146-1d6 // indirect ) - etcd v3 v1/v2 Go - x/net protobuf grpc 18
19 .1.11 go mod download module cache edit go.mod graph init , go.mod tidy module module vendor vendor verify HASH why 19
20 .1.12 / • replace tag • replace tag • HTTPS_PROXY go get • GOPROXY HTTPS_PROXY=socks5://127.0.0.1:1080 GOPROXY=file:///filesystem/path 20
21 .2 (Go2) 21
22 .2.1 func CopyFile(src, dst string) error { r, err := os.Open(src) if err != nil { return fmt.Errorf("copy %s %s: %v", src, dst, err) } defer r.Close() w, err := os.Create(dst) if err != nil { return fmt.Errorf("copy %s %s: %v", src, dst, err) } if _, err := io.Copy(w, r); err != nil { w.Close() os.Remove(dst) return fmt.Errorf("copy %s %s: %v", src, dst, err) } if err := w.Close(); err != nil { os.Remove(dst) return fmt.Errorf("copy %s %s: %v", src, dst, err) } 22 return nil
23 .2.2 if err != nil { return fmt.Errorf("copy %s %s: %v", src, dst, err) } v0, err := strconv.Atoi("123") if err != nil { return err } 23 v1 := v0*2
24 .2.3 - check & handle func CopyFile(src, dst string) error { handle err { return fmt.Errorf("copy %s %s: %v", src, dst, err) } r := check os.Open(src) defer r.Close() w := check os.Create(dst) handle err { 24
25 .2.4 check func printSum(a, b string) error { handle err { return err } fmt.Println("result:", check strconv.Atoi(x) + check strconv.Atoi(y)) return nil } 25
26 .2.5 handle & check func process(user string, files chan string) (n int, err error) { handle err { return 0, fmt.Errorf("process: %v", err) } // handler A for i := 0; i < 3; i++ { handle err { err = fmt.Errorf("attempt %d: %v", i, err) } // handler B handle err { err = moreWrapping(err) } // handler C check do(something()) // check 1: handler chain C, B, A } check do(somethingElse()) // check 2: handler chain A } 26
27 .2.6 27
28 .2.7 errors package errors // A Wrapper is an error implementation // wrapping context around another error. type Wrapper interface { // Unwrap returns the next error in the error chain. // If there is no next error, Unwrap returns nil. Unwrap() error Wapper } 28
29 .2.8 Is As Is : // err == io.ErrUnexpectedEOF if errors.Is(err, io.ErrUnexpectedEOF) { ... } As : // pe, ok := err.(*os.PathError) if pe, ok := errors.As(*os.PathError)(err); ok { ... }29