Upgrade to Pro — share decks privately, control downloads, hide ads and more …

«Новое в Go 1.10» — Алексей Палажченко (Percona)

Badoo Tech
March 07, 2018
8.9k

«Новое в Go 1.10» — Алексей Палажченко (Percona)

Выступление на Go 1.10 Release Party @ Badoo 24.02.2018.
Празднуем выход нового релиза: изменения в языке и в портах, в компиляторе и в инструментах, в runtime’е и в стандартной библиотеке, и, конечно, в скорости работы.​

Badoo Tech

March 07, 2018
Tweet

More Decks by Badoo Tech

Transcript

  1. License and Materials This presentation is licensed under the Creative

    Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/) licence. The materials for this presentation are available on GitHub: github.com/AlekSi/go-1.10-release-party (https://github.com/AlekSi/go-1.10-release-party) You are encouraged to remix, transform, or build upon the material, providing you distribute your contributions under the same license. This presentation is based on the one made by Florin Pățan. See GitHub link above for details.
  2. Go 1.10 Go 1.10 is released! Go 1.10 Announcement (https://blog.golang.org/go1.10)

    Go 1.10 is the 11th release in the Go 1 series. It follows from the previous version, Go 1.9, released on the 24th of August, 2016 (https://goo.gl/BUVc6Y) You can view the release notes here: Go 1.10 Release Notes (http://tip.golang.org/doc/go1.10)
  3. What's changed? So, what's happened in the last six months?

    Language Changes Ports & OS Support Performance Compiler & Tools Runtime CGO support Standard Library
  4. Language Changes There are no signi cant changes to the

    language speci cation. However, there are a couple of minor changes.
  5. Language Changes First, the compilers have been updated to allow

    the index expression x[1.0 << s] where s is an untyped constant package main func main() { const s = 10 a := make([]byte, 1.0<<s) // valid _ = a[1.0<<s] // in Go 1.9 accepted by go/types, not accepted by cmd/compile } In Go 1.9: prog.go:6:11: invalid operation: 1 << s (shift of type float64) In Go 1.10 panic: runtime error: index out of range
  6. Language Changes Method expressions such as struct{io.Reader}.Read, that the compilers

    already accepted, are now permitted by the language grammar package main import "io" type MyType struct{ io.Reader } func main() { m := MyType{} dst := make([]byte, 10) l, err := struct{ io.Reader }.Read(m, dst) _, _, _ = l, err, dst }
  7. Language Changes Go 1.10 output: go.struct { io.Reader }.Read: call

    to external function main.main: relocation target go.struct { io.Reader }.Read not defined main.main: undefined: "go.struct { io.Reader }.Read" We'll have to wait for Go 1.11, github.com/golang/go/issues/22444 (https://github.com/golang/go/issues/22444) to get this xed AND working.
  8. Ports & OS Support No new ports are present. Go

    1.10 now requires FreeBSD 10.3 or later Go needs the unreleased NetBSD 8, only GOARCH amd64 and 386 are working, arm port is still broken Go 1.10 is the last release that will run on: OpenBSD 6.0. Go 1.11 will require OpenBSD 6.2 OS X 10.8 Mountain Lion or OS X 10.9 Mavericks. Go 1.11 will require OS X 10.10 Yosemite or later Windows XP or Windows Vista. Go 1.11 will require Windows 7 or later
  9. Ports & OS Support On 32-bit MIPS systems, the new

    GOMIPS environment variable can be either hard oat (default) or soft oat and selects whether to use hardware instructions or software emulation for oating-point computations. As you'll almost certainly be cross compiling for MIPS from a larger machine this is how you'd use soft oat: % GOARCH=mips GOOS=linux GOMIPS=softfloat go build your/package/here
  10. Performance As always, the changes are so general and varied

    that precise statements about performance are di cult to make. Most programs should hopefully run a bit faster, due to speedups in the garbage collector, optimizations in the compiler and standard library. However no o cial or uno cial comparisons are out for now so it's hard to talk about performance for this release just yet.
  11. Compiler & Tools - The Great Caching go build now

    maintains a cache of recently built packages, separate from the installed packages in $GOROOT/pkg or $GOPATH/pkg. This means you do not need to run go build -i or go test -i any more, the go tool automatically caches previous builds results. Furthermore, go build command now detects out-of-date packages purely based on the content of source les, speci ed build ags, and metadata stored in the compiled packages. Modi cation times are no longer consulted or relevant.
  12. Compiler & Tools - The Great Caching The caching change

    does not apply only to go build but also to go test. To bene t from the caching of tests there are a few conditions that must be met: the results are successful the test executable and command line match a previous run the les and environment variables used by that run have not changed the go test command received a list of packages to test, go test ./... for example the test command line uses a subset of the test ags, -cpu, -list, -parallel, -run, -short, and -v To bypass the test cache, you need to run with -count=1 parameter. The output of the tests will also be modi ed due to caching. The run time of the tests will notify that the cached output is used, by displaying (cached) instead of the original test run time.
  13. Compiler & Tools - The Great Caching E.g. fresh copy

    of Terraform on a system without any installed package or cache: * rst go test ./... nished in about 3 minutes [...] FAIL github.com/hashicorp/terraform/config/module 6.173s ok github.com/hashicorp/terraform/dag 2.712s [...] second go test ./... nished in less than 40 seconds [...] FAIL github.com/hashicorp/terraform/config/module 7.060s ok github.com/hashicorp/terraform/dag (cached) [...] * compiled on Windows 10 with a i7 6700HQ (8 cores) and 32GB RAM
  14. Compiler & Tools - go test Caching is not the

    only new functionality gained by go test in this release. Select go vet checks will now run before your tests, to ensure that common mistakes do not make it in your code before tests are running. Another, long awaited change, is the ability of -coverpkg to nally compute the coverage of multiple packages: go test -coverpkg=all -coverprofile cover.out ./... The -failfast ag now will stop testing immediately on the rst failure, with the caveat that parallel tests are still allowed to continue until they nish.
  15. Compiler & Tools Windows now supports c-shared archives (aka Windows

    gets DLL support!) OSX now has support for Go plugins. Windows still doesn't, see issue #19282 (https://github.com/golang/go/issues/19282) .
  16. Compiler & Tools The go build ags: -asm ags, -gc

    ags, -gccgo ags, and -ld ags options now apply by default only to the packages listed directly on the command line. For example, go build -gcflags=-m mypkg passes the compiler the -m ag when building mypkg but not its dependencies.
  17. Compiler & Tools - gofmt Three index slice expressions containing

    complex expressions are now always formatted as slice[start+1 : stop : capacity] Single-method interface literals written on a single line, which are sometimes used in type assertions, are no longer split onto multiple lines. if c, ok := v.(interface { Close() error }); ok { } can become: if c, ok := v.(interface{ Close() error }); ok { }
  18. Compiler & Tools - gofmt If a composite literal would

    include a comment and only comments, then the comment(s) will now be indented. var _ = []T{/* lone comment */} var _ = []T{ /* lone comment */ } will become: var _ = []T{ /* lone comment */ } var _ = []T{ /* lone comment */ } Warning: The o cial stance is: do not use gofmt in your CI or risk errors due to subtle changes such as these, as gofmt is not covered by the same guarantees as Go itself.
  19. Compiler & Tools - go x go x now replaces

    imports from golang.org/x/net/context with context which will help you migrate your code to a Go 1.9+ compatible code by running: go tool fix -r context your/package
  20. Compiler & Tools Most important changes are under amd64 platform

    with 359 new instructions including the full instruction set for: AVX AVX2 BMI BMI2 F16C FMA3 SSE2 SSE3 SSSE3 SSE4.1 & SSE4.2
  21. Runtime If before, in nested calls, UnlockOSThread would need to

    be called only once to unlock the thread, now it will need to be called as many times as LockOSThread was called. You may have noticed the <autogenerated> frame (line) in the stack traces before. This is now hidden, unless a panic or other issue happens in it. This also means that if your code would call runtime.Caller with a certain number of skip frames, then this change will be a "breaking change" in its behaviour as the <autogenerated> frames will not be counted there either.
  22. Runtime Go 1.10 also introduces soft and hard goals (limits)

    for garbage collection. The soft limit is the current value of the GOGC while the hard limit is 10% higher than the soft limit. Heavy GC reliant applications (so far only benchmarks) shows that there’s an increase in the heap size of the application. These limits are not currently con gurable, see golang.org/cl/59970 (https://golang.org/cl/59970) for the discussion on it.
  23. CGO support C typedefs such as typedef X Y now

    are treated as Go aliases. You'll be able to use C.X and C.Y interchangeably in Go now, as if they would be Go aliases, type X = Y. You can now pass Go strings directly to C. This is done by declaring a C function in a Go le with a parameter type of the special type name _GoString_. To access the string length, you’ll need to call size_t _GoStringLen(_GoString_ s) an in order to get the pointer to the string contents, you’ll need use const char *_GoStringPtr(_GoString_ s). Some C types that were previously mapped to a pointer type in Go are now mapped to uintptr type. A couple of these types are CFTypeRef in Darwin's CoreFoundation framework and the jobject in Java’s JNI interface. You’ll need to initialize the values for the a ected types with 0 instead of nil. This is a breaking change but thankfully you can x this quickly and automatically by running go tool fix -r cftype your/package or go tool fix -r jni your/package
  24. Standard Library bu o - new Reader.Size and Writer.Size methods

    report the Reader's or Writer's underlying bu er size bytes - the Fields, FieldsFunc, Split, and SplitAfter each already returned slices pointing into the same underlying array as its input. Go 1.10 changes each of the returned subslices to have capacity equal to its length, so that appending to a subslice will not overwrite adjacent data in the original input. This is also a "breaking change" in the behavior of these functions and you might need to update your code. encoding/json - the Decoder adds a new method DisallowUnknownFields that causes it to report inputs with unknown JSON elds as a decoding error. The default behavior has always been to discard unknown elds encoding/json - Unmarshal can no longer decode into elds inside embedded pointers to unexported struct types, because it cannot initialize the unexported embedded pointer to point at fresh storage. Unmarshal now returns an error in this case. This means you may need to update your code or a "breaking change" will happen, which could be hidden if the code is not properly handling errors
  25. Standard library math/rand - the new math/rand.Shu e function and

    corresponding math/rand.* Rand.Shu e method shu e an input sequence * func ExampleShuffle() { rand.Seed(time.Now().UnixNano()) numbers := []byte("12345") rand.Shuffle(len(numbers), func(i, j int) { numbers[i], numbers[j] = numbers[j], numbers[i] }) for i := range numbers { fmt.Printf("%c ", numbers[i]) } // Output: // 3 4 1 5 2 } * Warning: remember to call rand.Seed() or the order will be predictable!
  26. Standard Library math - the new functions Round and RoundToEven

    round their arguments to the nearest oating-point integer; Round rounds a half-integer to its larger integer neighbor (away from zero) while RoundToEven rounds a half-integer to its even integer neighbor net - TCPListener and UnixListener now implement syscall.Conn, to allow setting options on the underlying le descriptor using syscall.RawConn.Control net - the Conn implementations returned by Pipe now support setting read and write deadlines net - the IPConn.ReadMsgIP, IPConn.WriteMsgIP, UDPConn.ReadMsgUDP, and UDPConn.WriteMsgUDP, methods are now implemented on Windows
  27. Standard Library net - the Conn and Listener implementations in

    this package now guarantee that when Close returns, the underlying le descriptor has been closed. In earlier releases, if the Close stopped pending I/O in other goroutines, the closing of the le descriptor could happen in one of those goroutines shortly after Close returned This is an important change as it now guarantees that you can do net.Listen(...) Close() net.Listen() and safely be able to acquire new connections. Before, in 1.9 or older, depending on timing and your OS, this could throw an error due to the port being still in use.
  28. Standard Library net/http - on the client side, an HTTP

    proxy, most commonly con gured by ProxyFromEnvironment, can now be speci ed as an https:// URL, meaning that the client connects to the proxy over HTTPS before issuing a standard, proxied HTTP request. Previously, HTTP proxy URLs were required to begin with http:// or socks5:// net/http - on the server side, FileServer and its single- le equivalent ServeFile now apply If-Range checks to HEAD requests. FileServer also now reports directory read failures to the Server's ErrorLog net/http - the content-serving handlers also now omit the Content-Type header when serving zero-length content net/http - ResponseWriter's WriteHeader method now panics if passed an invalid (non- 3-digit) status code net/http - Redirect now sets the Content-Type header before writing its HTTP response
  29. Standard Library net/url - ResolveReference now preserves multiple leading slashes

    in the target URL. Previously it rewrote multiple leading slashes to a single slash, which resulted in the http.Client following certain redirects incorrectly os - File adds new methods SetDeadline, SetReadDeadline, and SetWriteDeadline that allow setting I/O deadlines when the underlying le descriptor supports non-blocking I/O operations. The de nition of these methods matches those in net.Conn os - also matching net.Conn, File's Close method now guarantee that when Close returns, the underlying le descriptor has been closed unicode - the unicode package and associated support throughout the system has been upgraded from version 9.0 to Unicode 10.0, which adds 8,518 new characters, including four new scripts, one new property, a Bitcoin currency symbol, and 56 new emoji
  30. Standard library strings - a new type Builder is a

    replacement for bytes.Bu er for the use case of accumulating text into a string result. The Builder's API is a restricted subset of bytes.Bu er's that allows it to safely avoid making a duplicate copy of the data during the String method func ExampleBuilder() { var b strings.Builder for i := 3; i >= 1; i-- { fmt.Fprintf(&b, "%d...", i) } b.WriteString("ignition") fmt.Println(b.String()) // Output: 3...2...1...ignition }
  31. Standard library database/sql/driver - drivers that want to construct a

    sql.DB for their clients can now implement the Connector interface and call the new sql.OpenDB function, instead of needing to encode all con guration into a string passed to sql.Open database/sql/driver - drivers that implement ExecerContext no longer need to implement Execer; similarly, drivers that implement QueryerContext no longer need to implement Queryer database/sql/driver - to allow drivers to better isolate di erent clients using a cached driver connection in succession, if a Conn implements the new SessionResetter interface, database/sql will now call ResetSession before reusing the Conn for a new client
  32. Community - Upcoming Conferences GopherCon India 2018, Pune, India www.gophercon.in

    (http://www.gophercon.in) GopherCon Russia 2018, Moscow, Russia, gophercon-russia.ru (http://gophercon-russia.ru) GothamGo 2018, New York, USA, gothamgo.com (http://gothamgo.com) GopherCon SG 2018, Singapore, 2018.gophercon.sg (https://2018.gophercon.sg) GopherCon Europe, Reykjavik, Iceland, gophercon.is (https://gophercon.is) GopherCon Denver 2018, Denver, CO, gophercon.com (http://gophercon.com) GopherCon Brasil 2018, Florianópolis, Brazil, 2018.gopherconbr.org/en (https://2018.gopherconbr.org/en) GoLab 2018, Florence, Italy, golab.io (http://golab.io) dotGo 2019, Paris, France, 2019.dotgo.eu (https://2019.dotgo.eu)
  33. Community - Resources List of all Go conferences: github.com/golang/go/wiki/Conferences (#ZgotmplZ)

    List of all Go meetups: go-meetups.appspot.com (https://go-meetups.appspot.com) Resources for new speakers: github.com/golang/go/wiki/NewSpeakers (https://github.com/golang/go/wiki/NewSpeakers)