The Go team has released a Go 1.23 release candidate (go1.23rc1) on June 21, 2024 . This indicates that the final Go 1.23 release is likely to come soon.
Sorted Slices with SortedFunc
The new SortedFunc
method for slices allows you to sort a slice using a custom comparison function. This is a highly requested feature that will make it easier to work with sorted data in Go.
Here’s an example of how you can use SortedFunc
to sort a slice of structs by a custom field:
type Person struct {
Name string
Age int
}
func main() {
people := []Person{
{"Alice", 25},
{"Bob", 30},
{"Charlie", 20},
}
// Sort the slice by age using a custom comparison function
sort.SortedFunc(people, func(i, j int) bool {
return people[i].Age < people[j].Age
})
fmt.Println(people)
// Output: [{Charlie 20} {Alice 25} {Bob 30}]
}
In this example, we define a Person
struct with Name
and Age
fields. We then create a slice of Person
instances and use the sort.SortedFunc
method to sort the slice by the Age
field in ascending order.
The sort.SortedFunc
method takes two arguments: the slice to be sorted and a comparison function. The comparison function should return true
if the element at index i
should come before the element at index j
in the sorted slice.
Improved Timer Implementation
The implementation of channel-based timers created by time.NewTimer
, time.After
, time.NewTicker
, and time.Tick
has been improved in Go 1.23. Here’s an example that demonstrates the changes:
func main() {
// Create a new timer
timer := time.NewTimer(5 * time.Second)
// Stop the timer before it fires
timer.Stop()
// Reset the timer to a new duration
timer.Reset(10 * time.Second)
// Wait for the timer to fire
<-timer.C
fmt.Println("Timer fired!")
}
In this example, we create a new timer with a duration of 5 seconds, then immediately stop the timer. We then reset the timer to a new duration of 10 seconds and wait for it to fire.
The key changes in Go 1.23 are:
Unstopped timers and tickers can now be garbage collected: In previous versions of Go, unstopped timers and tickers would continue to consume system resources even if they were no longer being used. In Go 1.23, these resources can now be reclaimed by the garbage collector.
Timer channels are now synchronous (unbuffered): In previous versions of Go, the timer channels were buffered, which could lead to stale time values being delivered. In Go 1.23, the timer channels are now synchronous (unbuffered), which gives the
t.Reset
andt.Stop
methods stronger guarantees about the state of the timer.
These changes should improve the reliability and performance of channel-based timers in Go 1.23.
Other Notable Changes
Here are some other notable changes in Go 1.23:
Optimizations for DeepEqual on slice, array, and struct types
The reflect.DeepEqual
function, used to compare the deep equality of two values, has been optimized for slices, arrays, and struct types. This should improve the performance of DeepEqual
in cases where you need to compare complex data structures.
Instruction alignment optimizations for assembly routines
The Go compiler has been updated to optimize the alignment of instructions in assembly routines better. This can lead to performance improvements in low-level code that heavily uses assembly.
Potential changes to the math/big package
The math/big
package, which provides support for arbitrary-precision arithmetic, may see some changes in Go 1.23. The exact nature of these changes is still under discussion, but they are likely to improve the performance and usability of the package.
Refactoring of internal/cpu to allow the linker to prune unneeded lower-level CPU implementations
The internal/cpu
package, which provides information about the CPU architecture, has been refactored to allow the Go linker to more effectively prune unused lower-level CPU implementations. This can lead to smaller binary sizes in some cases.
Overall, Go 1.23 is shaping up to be an exciting release with several improvements and new features that will benefit Go developers. As always, it’s recommended to try out the release candidate and provide feedback to the Go team.