Golang generics spark excitement, anxiety in watershed release
Golang generics support is a landmark event in the language's evolution that some say could expand enterprise usage, while others are nervous about potential complexity.
The Go programming language community made support for generic types generally available this week in version 1.18, marking a turning point in its development.
Go, also known as Golang, was created at Google in 2007 with a focus on developer productivity in distributed web application environments. The language has been adopted by many programmers since then because of the clarity of its syntax and because it is usually easy for developers to learn quickly, compared with older programming languages such as Java and C++.
However, one of the key structural differences between Golang's syntax and that of others commonly used among enterprise programmers has long been a source of significant debate within the Go community: support for generic types within functions.
Until this week's release, Go has been strictly a strongly-typed language, meaning programmers must specify the type of parameter they want to use within a function. In other languages, including Java and C#, programmers can use generic types that don't require specifying types, such as numeric values, each time they write a function.
Go's creators were always open to generics support, but it has taken time to add it without compromising the fundamental values of the language around simplicity and balance performance tradeoffs.
"It's been a long time coming, and the FAQ has always said, 'It's very complicated and we want to get it right,'" said Brad Fitzpatrick, who was among the early members of the Go team at Google and is now a late-stage co-founder at Tailscale, a secure private network startup that contributed enhancements to the way Go handles IP addresses in version 1.18. "Somehow that turned into this mythos that 'Go hates generics; you don't need them; we'll never have them.'"
Golang generics support means more concise and efficient code in some cases, especially where applications must examine and precisely store data according to its structure. Widely used software frameworks with many contributors, such as the Kubernetes container orchestration project, are written in Go, and less repetitive code with generics could streamline collaboration. Fitzpatrick said he expects new additions to Go's standard library will take advantage of these efficiencies and make them reusable for developers who don't want to deal with the finer points of how generics are written.
Golang generics sea change comes with queasiness
Still, the fact that Go has existed for 15 years without generics support means that some of the language's early proponents are wary of the change.
Mat RyerEngineering director, Grafana Labs
"People love Go because of its simplicity -- specifically, its lack of features compared to other modern languages," said Mat Ryer, engineering director at open source observability vendor Grafana Labs. "So there's anxiety about adding big features like generics, [the potential for] features being misused, and code becoming difficult to read and maintain."
Go already has some support for the concept of polymorphism, in which a piece of code changes its behavior based on data input. However, that support has always been at runtime in Go -- rather than compile time -- until this week's release of version 1.18.
"Like anything, runtime polymorphism can be abused in code and make code hard to read, but if you're using it sparingly and where it's supposed to be used, the code can still be reasoned about fairly well," said William Kennedy, managing partner at mobile and web app development company Ardan Labs in Miami. He is also the author of a book, a blog and the Ultimate Go training programs.
"To have compile-time polymorphism, you need new syntax, which adds complexity to the code you're reading," Kennedy added. "Now you've got brackets, you've got extra parentheses, you've got things that are much harder for your brain to decode."
Kennedy said he doesn't plan to use Golang generics and is unlikely to teach students about them in his trainings. However, he said he still supports the change overall.
"This isn't about the current 10 years of Go developers, it's about the next 10 years," he said. "Java and C# rule the enterprise because of those languages' ability to build very large generic frameworks. My hope is that those developers start looking at Go now."
Grafana Labs' Ryer will approach generics with what he calls "a healthy anxiety," but isn't opposed to using them, eventually -- if sparingly.
"I won't use it for simple cases," he said. "I'd rather have copies of simple code than solve things generically, purely to optimize for ... readability. But when I'm doing nontrivial operations, or representing complex data structures, or in cases where I can tell a simpler story with the abstraction, I'll do it."
Fuzzing integration boosts Go test automation
Fuzz testing, a test automation technique that finds bugs in the form of "unknown unknowns" in code by bombarding it with random data inputs or "fuzz," has long been possible with Go through external utilities, but can now be invoked using a Go command. Fuzz tests that detect bugs can also automatically launch a built-in unit test under the 1.18 update.
This update, along with performance optimizations within Go 1.18 for Arm systems, pale in comparison to the significance of generics support, but are also important, experts said.
"Fuzzing integration expands the number of tests developers can do without a lot of extra work," said Arnal Dayaratna, analyst at IDC. "The whole field of testing is going through its own evolution and disruption, and machine learning algorithms are increasingly taking responsibility for it. This could streamline that."
Beth Pariseau, senior news writer at TechTarget, is an award-winning veteran of IT journalism. She can be reached at [email protected] or on Twitter @PariseauTT.