rss linkedin linkedin gitlab github twitter mastodon instagram
What is new in Go 1.19?
Aug 05, 2022

Disclaimer: This post includes Amazon affiliate links. If you click on one any them and you make a purchase I’ll earn a commission, notice however your final price is not affected at all by using those links.

It’s that time again! Another minor version of Go was released, superseeding Go 1.18. Released a few days ago on August 2, 2022, this is Go 1.19! 💥 🎉 🎊


What is new?

Compared to the 1.18 release there are no changes to the language but rather additions to the runtime, libraries and toolchain. There are a bunch of new features but in this post I will call out four that I believe are important to notice.


Updating

Depending on your development platform and the production system you use for running your services you may be already able to use Go 1.19. If not the official page always prepackaged versions for different platforms.

For more concrete examples:


Doc comments

The code used for this post is available on Github.

In Go 1.19 comments now support links, lists and clearer headings; this change is supported by gofmt so comments are formatted accordingly to those rules. For example:

 1// Package comments demonstrates how to use the new doc rendering feature added
 2// in Go 1.19 that is similar to Markdown.
 3//
 4// # What is included in this dummy package?
 5//
 6// A simple function called [HelloWorld], that's it.
 7package comments
 8
 9// HelloWorld returns a welcome message
10//
11// Depending on who you ask something important, other examples:
12//
13//  1. hola
14//  2. welcome!
15//
16// You can also search for other welcome messages on [Google]
17//
18// [Google]: https://google.com/
19func HelloWorld() string {
20	return "hello world"
21}

Will look like this:

Go 1.19 Documentation

Vet error

The code used for this post is available on Github.

go vet will check errors.As in cases where the second argument is of type error, for example:

 8func main() {
 9	var newErr = errors.New("sentinel")
10	err := errors.New("foo")
11
12	if errors.As(err, &newErr) {
13		fmt.Println("error here!")
14	}
15}

Displays:

# command-line-arguments
./main.go:12:5: second argument to errors.As should not be *error

Atomic types

The code used for this post is available on Github.

The package sync/atomic includes new atomic types for: Bool, Int32, Int64, Uint32, Uint64, Uintptr, and Pointer. So code written in versions before 1.19:

 8func main() {
 9	var i int64
10	atomic.AddInt64(&i, 10)
11	fmt.Println(atomic.LoadInt64(&i))
12}

Can be rewritten as:

 8func main() {
 9	var i atomic.Int64
10	i.Add(10)
11	fmt.Println(i.Load())
12}

flag.TextVar

The code used for this post is available on Github.

In flag there is a new function called TextVar that supports encoding.TextUnmarshaler allowing command-line flag variables to support any type behind the scenes, for example big.Int, netip.Addr, and time.Time:

 9func main() {
10	var ip net.IP
11
12	// Type "net.IPv4" implements the interface "encoding.TextMarshaler",
13	// specifically the method "MarshalText"
14	flag.TextVar(&ip, "ip", net.IPv4(192, 168, 0, 100), "`IP address` to parse")
15	flag.Parse()
16
17	fmt.Printf("Value: %v\n", ip)
18}

Running ./main -ip blah will fail because it’s not a valid IP Address, but using something like ./main -ip 127.0.0.1 should work.


Conclusion

Compared to previous version there are not a big significant changes to the language but still every new thing is always welcome. Looking forward to Go 1.20!

If you’re looking to sink your teeth into more Go-related topics I recommend the following books:


Back to posts