The State of Go_2017_May

2017/05 Golang 发展现状 from talks.golang.org
展开查看详情

1.1/24/2019 The State of Go The State of Go Where we are in May 2017 Francesc Campoy Google Developer Advocate https://talks.golang.org/2017/state-of-go-may.slide#1 1/39

2.1/24/2019 The State of Go Recording A recording of this talk is available here (https://www.youtube.com/watch?v=dyvpF0jF3AY&list=PLtLJO5JKE5YAeVbgwCkgUWyOBsMvZu4UB&index=1) . 2 https://talks.golang.org/2017/state-of-go-may.slide#1 2/39

3.1/24/2019 The State of Go Time ies Go 1.7 is already 9 months old! Go 1.8 was released on February 16th. On May 1st we entered the release freeze for Go 1.9 Go 1.9 will be released early August. 3 https://talks.golang.org/2017/state-of-go-may.slide#1 3/39

4.1/24/2019 The State of Go Notes The slides are available on talks.golang.org/2017/state-of-go-may.slide (https://talks.golang.org/2017/state-of-go- may.slide) Most of the code examples won't run except locally and using tip. The playground runs Go 1.8. 4 https://talks.golang.org/2017/state-of-go-may.slide#1 4/39

5.1/24/2019 The State of Go Agenda Changes since Go 1.8: The Language The Standard Library The Runtime The Tooling The Community 5 https://talks.golang.org/2017/state-of-go-may.slide#1 5/39

6.1/24/2019 The State of Go Changes to the language 6 https://talks.golang.org/2017/state-of-go-may.slide#1 6/39

7.1/24/2019 The State of Go Codebase Refactoring (with help from Go) Article written by Russ Cox link (https://talks.golang.org/2016/refactor.article) Add new API Code repairs Remove old API 7 https://talks.golang.org/2017/state-of-go-may.slide#1 7/39

8.1/24/2019 The State of Go Gradual Code Repair In reality, atomically changing all usages of an API is often impossible. Stage 1 Add new API Stage 2 Code repairs Code repairs Code repairs Code repairs Stage 3 Remove old API 8 https://talks.golang.org/2017/state-of-go-may.slide#1 8/39

9.1/24/2019 The State of Go An example Imagine we created a new package net/http/status. First: create the new API package status const OK = http.StatusOK Second: change each usage of http.StatusOK by status.OK. if res.StatusCode != http.StatusOK { if res.StatusCode != status.OK { Third: remove the old API 9 https://talks.golang.org/2017/state-of-go-may.slide#1 9/39

10.1/24/2019 The State of Go Another example Let's rename http.Get to http.DoGetPleaseAndThanks. First: create the new API func DoGetPleaseAndThanks(url string) (*http.Response, error) { return Get(url) } Second: change each usage of http.Get to http.DoGetPleaseAndThanks. res, err := http.Get("https://golang.org") res, err := http.DoGetPleaseAndThanks("https://golang.org") Third: remove the old API 10 https://talks.golang.org/2017/state-of-go-may.slide#1 10/39

11.1/24/2019 The State of Go One last example Let's move http.Client to http.Applicant. First: create the new API type Applicant Client Applicant has no methods. Both types are convertible. type Applicant struct { Client } Applicant has all the methods of Client The types are not convertible. 11 https://talks.golang.org/2017/state-of-go-may.slide#1 11/39

12.1/24/2019 The State of Go Alias declarations An alias declaration is a new kind of type declaration. type Applicant = http.Client Both types are equivalent and completely interchangeable. type conversion is not needed can't declare methods on the alias declaration type Applicant = http.Client func main() { fmt.Printf("%T", Applicant{}) } Run 12 https://talks.golang.org/2017/state-of-go-may.slide#1 12/39

13.1/24/2019 The State of Go Quaternions issue #19813(https://golang.org/issue/19813) 13 https://talks.golang.org/2017/state-of-go-may.slide#1 13/39

14.1/24/2019 The State of Go The Standard library 14 https://talks.golang.org/2017/state-of-go-may.slide#1 14/39

15.1/24/2019 The State of Go A Twitter Poll twitter poll(https://twitter.com/francesc/status/863514399486623744) 15 https://talks.golang.org/2017/state-of-go-may.slide#1 15/39

16.1/24/2019 The State of Go math/bits Package bits (https://tip.golang.org/pkg/math/bits/#Len16) implements bit counting and manipulation functions for the predeclared unsigned integer types. Added to the standard library with proposal #18616 (https://golang.org/issue/18616) . LenXX, OnesCountXX ReverseXX, ReverseBytesXX RotateLeftXX LeadingZerosXX, TrailingZerosXX fmt.Printf("%d (%b) has %d bits set to one\n", n, n, bits.OnesCount(n)) fmt.Printf("%d reversed is %d\n", n, bits.Reverse(n)) fmt.Printf("%d can be encoded in %d bits\n", n, bits.Len(n)) Run 16 https://talks.golang.org/2017/state-of-go-may.slide#1 16/39

17.1/24/2019 The State of Go sync.Map A new type has been added to the sync package with proposal #18177 (https://golang.org/issue/18177) . sync.Map (https://tip.golang.org/pkg/sync/#Map) is a concurrent map with amortized-constant-time loads, stores,and deletes. the zero map is valid it must not be copied (use pointers) 17 https://talks.golang.org/2017/state-of-go-may.slide#1 17/39

18.1/24/2019 The State of Go sync.Map code sample func main() { var m sync.Map for i := 0; i < 3; i++ { go func(i int) { for j := 0; ; j++ { m.Store(i, j) } }(i) } for i := 0; i < 10; i++ { m.Range(func(key, value interface{}) bool { fmt.Printf("%d: %d\t", key, value) return true }) fmt.Println() time.Sleep(time.Second) } } Run 18 https://talks.golang.org/2017/state-of-go-may.slide#1 18/39

19.1/24/2019 The State of Go html/template panic on prede ned escaper What do you expect this code (https://play.golang.org/p/-z5rZilH1F) to print? type Foo struct{ Bar string } func main() { tmpl, err := template.New("home").Parse(` <a title={{.Bar | html}}> `) if err != nil { log.Fatalf("could not parse: %v", err) } foo := Foo{"haha onclick=evil()"} if err := tmpl.Execute(os.Stdout, foo); err != nil { log.Fatalf("could not execute: %v", err) } } Run Prede ned escapers in html template create a security concern. Since 1.9 Execute will panic. 19 https://talks.golang.org/2017/state-of-go-may.slide#1 19/39

20.1/24/2019 The State of Go os.Exec Let's imagine that we have a command getenv that prints an environment variable using os.Getenv. func main() { if len(os.Args) != 2 { fmt.Printf("use %s varname\n", os.Args[0]) os.Exit(1) } fmt.Println(os.Getenv(os.Args[1])) } We can run it as follows: $ foo=bar getenv foo bar 20 https://talks.golang.org/2017/state-of-go-may.slide#1 20/39

21.1/24/2019 The State of Go os.Exec What do you expect this code to print? func main() { cmd := exec.Command("getenv", "foo") cmd.Env = append(os.Environ(), "foo=newbar") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { log.Fatal(err) } } bar, or newbar? 21 https://talks.golang.org/2017/state-of-go-may.slide#1 21/39

22.1/24/2019 The State of Go another Twitter poll Twitter poll(https://twitter.com/francesc/status/863791506934472705) 22 https://talks.golang.org/2017/state-of-go-may.slide#1 22/39

23.1/24/2019 The State of Go os.Exec Cmd.Start now removes duplicates of environment variables, keeping the last one. This code does what one expects: cmd := exec.Command("prog") cmd.Env = append(os.Environ(), "FOO=bar") 23 https://talks.golang.org/2017/state-of-go-may.slide#1 23/39

24.1/24/2019 The State of Go The Runtime 24 https://talks.golang.org/2017/state-of-go-may.slide#1 24/39

25.1/24/2019 The State of Go Benchmarks note: values over 1.0 mean tip is faster note: uno cial benchmark ran on my laptop while playing YouTube videos 25 https://talks.golang.org/2017/state-of-go-may.slide#1 25/39

26.1/24/2019 The State of Go More runtime Garbage Collector New algorithm for large object allocation Better performance and increased determinism for large (+50GB) heaps DWARF (https://en.wikipedia.org/wiki/DWARF) Ongoing e ort to improve the generated DWARF information. This will help debuggers, among other tools. 26 h li l https://talks.golang.org/2017/state-of-go-may.slide#1 26/39

27.1/24/2019 The State of Go The Tooling 27 il b https://talks.golang.org/2017/state-of-go-may.slide#1 ! 27/39

28.1/24/2019 The State of Go go compiler: better errors! Better error messaging for Allman style braces. package main func main() { fmt.Println("that ain't gonna compile") } With go 1.8: fail/main.go:4: syntax error: unexpected semicolon or newline before { With go 1.9: fail/main.go:3:6: missing function body for "main" fail/main.go:4:1: syntax error: unexpected semicolon or newline before { 28 il https://talks.golang.org/2017/state-of-go-may.slide#1 d l df 28/39

29.1/24/2019 The State of Go go compiler: more modular and faster The compiler has been refactored into multiple packages. cmd/go/internal/... Issue #17639 (https://golang.org/issue/17639) made parsing concurrent. The compiler is faster as a result. 29 t t https://talks.golang.org/2017/state-of-go-may.slide#1 29/39