In the recently released Go 1.9, the most significant change is support for gradual code repair by declaring type aliases. Go 1.9 also improves the garbage collector and compiler.
According to Google engineer Russ Cox, progressive code fixes are a useful way to refactor code and are valuable for large code bases. In short, the goal of progressive code repair is to accomplish a larger refactoring in a series of steps, meaning that all changes are not done in one automated commit, but rather divided into multiple commits. Atomic refactoring is often conceptually simple, but if the code base is large, it can result in large code commits that are difficult to review and merge. With progressive code repair, we can refactor the code in three steps: First, introduce a new API, which should coexist with the old API, so we don’t have to immediately change all the code that uses the old API; Then, we replace everything that uses the old API with the new API; Finally, remove the old API.
To enable progressive code fixing, you must create an alternative name for constants, functions, variables, and types. Go now allows you to declare type aliases as follows:
__Mon Sep 04 2017 17:55:58 GMT+0800 (CST)____Mon Sep 04 2017 17:55:58 GMT+0800 (CST)__type OldAPI = NewPackage.API__Mon Sep 04 2017 17:55:58 GMT+0800 (CST)____Mon Sep 04 2017 17:55:58 GMT+0800 (CST)__Copy the code
In this case, all references to OldAPI will use the refactored type. If readers are interested in more discussion of progressive code fixes, Russ Cox’s presentation is not to be missed.
According to Google engineer Francesc Campoy, most of the engineering effort for Go 1.9 went into improving the runtime, core libraries, and tools. The most significant changes include:
-
Go’s garbage collection provides better performance thanks to the fact that some library functions trigger concurrent garbage collection, which blocks the goroutine rather than the entire program. In addition, heap memory allocation for large objects has been significantly improved.
-
The Go 1.9 compiler can compile functions in the same package in parallel. In previous versions of the compiler, parallel compilation of functions in different packages was already supported.
-
On the core library side, Go 1.9 makes the Time package safer to use with the help of monotonic time tracking. In this way, Time comparison will be easier even if there is wall clock adjustment. In addition, in the SYNC package, the new Map type provides a thread-safe concurrent Map with amortized constant-time load, store, and delete capabilities.
To see all the changes to Go 1.9, refer to the release documentation.
Introduce Type Aliases, Improves Runtime and Tooling