What is new in Go 1.8

  • View
    9.632

  • Download
    0

  • Category

    Software

Preview:

Citation preview

What is new in Go 1.8

@huazhihao

Agenda● Language

● Runtime

● Standard Library

● Tool Chain

Language● Conversion ignoring struct tags

● Alias declaration

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

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

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

Runtime● gc pause (< 1ms)

● build (15% speedup)

● defer (10%-35% speedup)

● cgo (45% speedup)

GC pause down to 1ms

1.6 40ms1.5 300ms

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

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

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

Compile speed comparison

Faster defer

(µs)

Faster cgo

ns/op

Standard Library● HTTP Graceful Shutdown

● HTTP2 Server Push

● TLS Additions

● SQL Additions

● Inline Slice Sorting

● Import path of context

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

}()

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

TLS Additions● Support for ChaCha20-Poly1305 based cipher suites

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

● More flexible config APIs

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)

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

})

Sorting Benchmark

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

Tool Chain● Plugins (Shared libraries)

● Default GOPATH

● go bug

● Smarter go vet

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

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

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

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

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)

}

Thank you

i@huazhihao.com

Recommended