Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

What is new in Go 1.8

13,296 views

Published on

What is new in Go 1.8

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

What is new in Go 1.8

  1. 1. What is new in Go 1.8 @huazhihao
  2. 2. Agenda ● Language ● Runtime ● Standard Library ● Tool Chain
  3. 3. Language ● Conversion ignoring struct tags ● Alias declaration
  4. 4. Conversion ignoring struct tags type X struct { Name string Num int } var Y struct { Name string `json:"name"` Num int `json:"num"` }
  5. 5. Alias declaration type Foo => pkg.Bar An alternate syntax in 1.9 ? AliasSpec = identifier "=>" PackageName "." identifier . const C => pkg.C type T => pkg.T var V => pkg.V func F => pkg.F
  6. 6. Runtime ● gc pause (< 1ms) ● build (15% speedup) ● defer (10%-35% speedup) ● cgo (45% speedup)
  7. 7. GC pause down to 1ms 1.6 40ms 1.5 300ms
  8. 8. STW Stack Rescanning Replaced by a hybrid write barrier combines: ● a Yuasa-style deletion write barrier ● a Dijkstra-style insertion write barrier An improved implementation of write barrier merged in dev branch reduces the gc pause reliably to 100µs(GOMAXPROCS=12).
  9. 9. ARM SSA Support SSA(Static single assignment) form is widely used in modern compilers Go didn’t have an SSA representation in its internals because it's derived from an old C compiler which predated SSA. SSA was introduced into the x86/64 platforms in Go 1.7 and immediately made ● 5–35% speedup ● 20–30% reduction in binary size
  10. 10. Static single assignment form in 1 minute y = 1 y = 2 x = y ↓ y1 = 1 y2 = 2 x1 = y2 Benefits ● constant propagation ● value range propagation ● sparse conditional constant propagation ● dead code elimination ● global value numbering ● partial redundancy elimination ● strength reduction ● register allocation
  11. 11. Compile speed comparison
  12. 12. Faster defer (µs)
  13. 13. Faster cgo ns/op
  14. 14. Standard Library ● HTTP Graceful Shutdown ● HTTP2 Server Push ● TLS Additions ● SQL Additions ● Inline Slice Sorting ● Import path of context
  15. 15. HTTP Graceful Shutdown quit := make(chan os.Signal) signal.Notify(quit, os.Interrupt) srv := &http.Server{Addr: ":8080", Handler: http.DefaultServeMux} go func() { <-quit log.Println("Shutting down server...") if err := srv.Shutdown(context.Background()); err != nil { log.Fatalf("could not shutdown: %v", err) } }()
  16. 16. HTTP2 Server Push http.Handle("/static", http.FileServer(http.Dir("./static"))) http.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) { if p, ok := w.(http.Pusher); ok { p.Push("/static/style.css", nil) p.Push("/static/image.png", nil) } w.Header().Set("Content-Type", "text/html") w.Write([]byte(` <link href="/static/style.css" rel="stylesheet" /> <img src="/static/image.png" />`)) }) http.ListenAndServeTLS(":4430", "cert.pem", "key.pem", nil) Stream Frame /static/style.css PUSH_PROMISE /static/image.png PUSH_PROMISE /index.html HEADERS /index.html DATA /static/style.css HEADERS /static/style.css DATA /static/image.png HEADERS /static/image.png DATA
  17. 17. TLS Additions ● Support for ChaCha20-Poly1305 based cipher suites ● CipherSuites automatically selected based on hardware support availability if not specified ● More flexible config APIs
  18. 18. SQL Additions ● Cancelable queries ctx, cancel := context.WithCancel(context.Background()) ● Visible database types type RowsColumnTypeDatabaseTypeName interface { Rows ColumnTypeDatabaseTypeName(index int) string } ● Multiple result sets rows.NextResultSet() ● Ping can hit server Conn.Ping() or Conn.PingContext(ctx) ● Named parameters sql.Named("Name", value) ● Transaction isolation BeginTx(ctx context.Context, opts *TxOptions)
  19. 19. Inline Slice Sorting sort.Sort: type ByKey []Item func (items ByKey) Len() int { … //Len implementation } func (items ByKey) Swap(i, j int) { ... //Swap implementation } func (items ByKey) Less(i, j int) bool { … //Less implementation } sort.Sort(ByKey(items)) sort.Slice: sort.Slice(items, func(i, j int) bool { //Less implementation })
  20. 20. Sorting Benchmark
  21. 21. Import path of context -import "golang.org/x/net/context" +import "context" Fix the import by running below on your code base: # dry run go tool fix -diff -force=context PATH # overwrite go tool fix -force=context PATH
  22. 22. Tool Chain ● Plugins (Shared libraries) ● Default GOPATH ● go bug ● Smarter go vet
  23. 23. Plugins (Shared libraries) plugin.go: package shared import "fmt" var V int func F() { fmt.Printf("Hello, number %dn", V) } main.go: package main import "plugin" func main() { p, err := plugin.Open("plugin.so") v, err := p.Lookup("V") f, err := p.Lookup("F") *v.(*int) = 7 f.(func())() // prints "Hello, number 7" } plugin.so: $ go build -buildmode=plugin plugin.go
  24. 24. Default GOPATH When GOPATH is not defined, the runtime will use: ● $HOME/go on Unix ● %USERPROFILE%go on Windows
  25. 25. go bug The easiest way to file a new issue: Run `go bug` Your browser will open https://github.com/golang/go/issues/new with system details prefilled
  26. 26. Smarter go vet package main import ( "io" "log" "net/http" "os" ) func main() { res, err := http.Get("https://golang.org") defer res.Body.Close() if err != nil { log.Fatal(err) } io.Copy(os.Stdout, res.Body) }
  27. 27. Thank you i@huazhihao.com

×