27
What is new in Go 1.8 @huazhihao

What is new in Go 1.8

Embed Size (px)

Citation preview

Page 1: What is new in Go 1.8

What is new in Go 1.8

@huazhihao

Page 2: What is new in Go 1.8

Agenda● Language

● Runtime

● Standard Library

● Tool Chain

Page 3: What is new in Go 1.8

Language● Conversion ignoring struct tags

● Alias declaration

Page 4: What is new in Go 1.8

Conversion ignoring struct tagstype X struct { Name string Num int}

var Y struct { Name string `json:"name"` Num int `json:"num"`}

Page 5: What is new in Go 1.8

Alias declarationtype Foo => pkg.Bar

An alternate syntax in 1.9 ?

AliasSpec = identifier "=>" PackageName "." identifier .

const C => pkg.Ctype T => pkg.Tvar V => pkg.V func F => pkg.F

Page 6: What is new in Go 1.8

Runtime● gc pause (< 1ms)

● build (15% speedup)

● defer (10%-35% speedup)

● cgo (45% speedup)

Page 7: What is new in Go 1.8

GC pause down to 1ms

1.6 40ms1.5 300ms

Page 8: What is new in Go 1.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).

Page 9: What is new in Go 1.8

ARM SSA SupportSSA(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

Page 10: What is new in Go 1.8

Static single assignment form in 1 minute

y = 1y = 2x = y

y1 = 1y2 = 2x1 = y2

Benefits

● constant propagation● value range propagation● sparse conditional constant propagation● dead code elimination● global value numbering● partial redundancy elimination● strength reduction● register allocation

Page 11: What is new in Go 1.8

Compile speed comparison

Page 12: What is new in Go 1.8

Faster defer

(µs)

Page 13: What is new in Go 1.8

Faster cgo

ns/op

Page 14: What is new in Go 1.8

Standard Library● HTTP Graceful Shutdown

● HTTP2 Server Push

● TLS Additions

● SQL Additions

● Inline Slice Sorting

● Import path of context

Page 15: What is new in Go 1.8

HTTP Graceful Shutdownquit := make(chan os.Signal)signal.Notify(quit, os.Interrupt)

srv := &http.Server{Addr: ":8080", Handler: http.DefaultServeMux}go func() {

<-quitlog.Println("Shutting down server...")if err := srv.Shutdown(context.Background()); err != nil {

log.Fatalf("could not shutdown: %v", err)}

}()

Page 16: What is new in Go 1.8

HTTP2 Server Pushhttp.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

Page 17: What is new in Go 1.8

TLS Additions● Support for ChaCha20-Poly1305 based cipher suites

● CipherSuites automatically selected based on hardware support availability if not specified

● More flexible config APIs

Page 18: What is new in Go 1.8

SQL Additions● Cancelable queries

ctx, cancel := context.WithCancel(context.Background())● Visible database types

type RowsColumnTypeDatabaseTypeName interface {RowsColumnTypeDatabaseTypeName(index int) string

}

● Multiple result setsrows.NextResultSet()

● Ping can hit serverConn.Ping() or Conn.PingContext(ctx)

● Named parameterssql.Named("Name", value)

● Transaction isolation BeginTx(ctx context.Context, opts *TxOptions)

Page 19: What is new in Go 1.8

Inline Slice Sortingsort.Sort:

type ByKey []Itemfunc (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

})

Page 20: What is new in Go 1.8

Sorting Benchmark

Page 21: What is new in Go 1.8

Import path of context-import "golang.org/x/net/context"+import "context"

Fix the import by running below on your code base:

# dry rungo tool fix -diff -force=context PATH # overwritego tool fix -force=context PATH

Page 22: What is new in Go 1.8

Tool Chain● Plugins (Shared libraries)

● Default GOPATH

● go bug

● Smarter go vet

Page 23: What is new in Go 1.8

Plugins (Shared libraries)plugin.go:

package shared

import "fmt"var V intfunc F() { fmt.Printf("Hello, number %d\n", 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) = 7f.(func())() // prints "Hello, number 7"

}

plugin.so:

$ go build -buildmode=plugin plugin.go

Page 24: What is new in Go 1.8

Default GOPATHWhen GOPATH is not defined, the runtime will use:

● $HOME/go on Unix● %USERPROFILE%\go on Windows

Page 25: What is new in Go 1.8

go bugThe 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

Page 26: What is new in Go 1.8

Smarter go vetpackage mainimport (

"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)

}