Go 1.8 includes improvements to the language, runtime, standard library, and tool chain. Key updates include faster garbage collection pauses below 1ms, 15% faster builds, ignoring struct tags during conversions, alias declarations using =>, HTTP graceful shutdown, HTTP/2 server push, TLS and SQL additions, and plugins/shared libraries. The default GOPATH is now set if undefined and go vet was made smarter.
4. Conversion ignoring struct tags
type X struct {
Name string
Num int
}
var Y struct {
Name string `json:"name"`
Num int `json:"num"`
}
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
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. 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. 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
14. Standard Library
● HTTP Graceful Shutdown
● HTTP2 Server Push
● TLS Additions
● SQL Additions
● Inline Slice Sorting
● Import path of context
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. 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. 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. 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)
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. Tool Chain
● Plugins (Shared libraries)
● Default GOPATH
● go bug
● Smarter go vet
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. Default GOPATH
When GOPATH is not defined, the runtime will use:
● $HOME/go on Unix
● %USERPROFILE%go on Windows
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. 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)
}