Go 1.8 (2017-02-16)
Go 1.8 was released on February 16, 2017. This release maintains the Go 1 compatibility promise while bringing significant performance improvements (especially GC and defer), as well as important features like HTTP/2 Server Push, HTTP Server graceful shutdown, and a plugin system.
Major Changes
Language
-
Struct Conversion Ignoring Tags: When explicitly converting one struct type to another, if they differ only in field tags, the conversion is now allowed directly without requiring field tags to match exactly.
type T1 struct {
X int `json:"foo"`
}
type T2 struct {
X int `json:"bar"`
}
var v1 T1
var v2 T2
v1 = T1(v2) // Illegal before Go 1.8, now legal
Toolchain
-
Plugin System: Go 1.8 introduced preliminary support for dynamically loading shared libraries (currently Linux only). The
pluginpackage allows Go programs to load Go packages compiled as plugins (.sofiles) at runtime and look up exported symbols (variables or functions).p, err := plugin.Open("myplugin.so")
f, err := p.Lookup("MyFunction") -
Default GOPATH: If the
GOPATHenvironment variable is not set, Go now uses a default value:- Unix:
$HOME/go - Windows:
%USERPROFILE%\go
- Unix:
Runtime
- Garbage Collection (GC): GC pause times are further reduced, typically under 100 microseconds, and even as low as 10 microseconds. This is due to the elimination of "stop-the-world" stack rescanning.
- Defer Overhead: The overhead of
deferfunction calls is reduced by about half. - Cgo Overhead: The overhead of calling C code from Go is also reduced by about half.
mutexProfiling: Support for profiling mutex contention (go test -mutexprofile).
Standard Library
-
HTTP/2 Server Push: The
net/httppackage added support for HTTP/2 Server Push. This allows the server to push resources before the client requests them.http.ResponseWriternow implements thePusherinterface.if pusher, ok := w.(http.Pusher); ok {
// Push is supported.
if err := pusher.Push("/style.css", nil); err != nil {
log.Printf("Failed to push: %v", err)
}
} -
HTTP Server Graceful Shutdown:
http.Serveradded theShutdownmethod, supporting graceful server shutdown.Shutdownstops listening for new requests but allows currently processing requests to complete before closing the server.ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
} -
sort.Slice: Thesortpackage added theSlicefunction, making it much simpler to sort slices without defining a new type to implementsort.Interface.sort.Slice(people, func(i, j int) bool {
return people[i].Name < people[j].Name
}) -
Context Support Extension: Support for
context.Contexthas been extended to more standard libraries:database/sql: AddedQueryContext,ExecContext,PrepareContext, etc., allowing database operations to be cancelled via Context.net.Resolver: DNS query methods now accept Context.
-
encoding/base64: AddedStrictmode to strictly check padding bits. -
encoding/json:Marshalnow supports floating-point number formatting consistent with ES6.
References
For more details, please refer to the official release notes: Go 1.8 Release Notes