> Markdown version of [/videos/435-go-generics](https://www.wearedevelopers.com/videos/435-go-generics). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Go Generics Are you sacrificing compile-time safety for generic flexibility? Master Go 1.18 type constraints to reduce repetitive boilerplate without losing robust validation. - **Speakers:** Nils Kasseckert - **Event:** World Congress 2022 - **Published:** June 15, 2022 - **Duration:** 25:08 - **URL:** https://www.wearedevelopers.com/videos/435-go-generics ## Summary Introduced in Go 1.18, generic programming transitions the language from rigidly typed, repetitive functions toward flexible, type-independent code. The core mechanism introduces type parameters within square brackets, allowing developers to build functions and structs that accept customized type sets. This update shifts the fundamental perspective of interfaces in Go; rather than merely defining method sets, interfaces can now act as type constraints defining a specific, permissible set of types. To streamline implementation, Go provides predefined constraints for common groupings like integers and floats, while the new tilde symbol syntax crucially allows these constraints to embrace custom underlying subtypes. Furthermore, the introduction of type inference—both for function arguments and constraints—cleans up syntax by enabling the compiler to deduce types automatically, eliminating the need to explicitly pass type arguments in most function calls. However, migrating to generic patterns introduces specific architectural trade-offs. Using the newly introduced `any` keyword (an alias for the empty interface) allows broad flexibility, such as parsing generic YAML structures, but inherently sacrifices compile-time type safety. To retain strict validation at compile time without completely losing generic flexibility, developers must evaluate practical workarounds, such as employing traditional marker interfaces. Ultimately, while Go generics drastically reduce boilerplate and improve readability, engineering teams must carefully balance this new flexibility against the potential loss of robust compile-time checks. **Keywords:** go generics programming, go 1.18 features, type parameter syntax, interface type sets, type constraints implementation, predefined integer constraint, underlying type tilde syntax, function argument type inference, constraint type inference, any keyword usage, compile-time type safety, marker interface patterns, generic struct parsing, generic yaml loader, empty interface aliasing ## Chapters 1. **Introduction to generic programming in modern Go** (00:00) — Writing type-independent code relies on newly introduced parameters, sets, and inference features. 1. **Defining and using type parameters in functions** (01:54) — Declaring type parameters in square brackets eliminates the need to duplicate functions for different data types. 1. **Implementing type constraints to restrict parameter types** (05:26) — Applying prefabricated constraints successfully restricts generic functions to specific data types while preserving compile-time validation. 1. **Understanding interfaces as type sets representing underlying types** (07:32) — Reconceptualizing interfaces as collections of types enables broader compatibility for custom underlying structures. 1. **Utilizing the tilde symbol and nested type constraints** (10:08) — Applying the tilde symbol and nested constraints effectively defines custom collection boundaries alongside the any alias. 1. **Applying function argument type inference in generics** (14:02) — Relying on compiler inference allows developers to omit explicit type arguments during standard function calls. 1. **Leveraging constraint type inference for complex parameter relationships** (16:21) — Resolving complex parameter dependencies relies on constraint type inference to deduce precise structural relationships from interconnected interfaces. 1. **Practical examples and limitations of the any constraint** (19:33) — Implementing marker interfaces effectively resolves compile-time safety limitations when building generic struct loaders with the any alias. 1. **Summary of generics and Q&A on type safety** (23:24) — Balancing generic implementations involves understanding core language mechanics alongside structural trade-offs regarding compile-time type safety. ## Related Moments - [Introduction to TypeScript generics for type safety](https://www.wearedevelopers.com/videos/82-hello-generics-goodbye-boilerplate) (from "Hello generics, goodbye boilerplate!") - [Multi paradigm language design principles in Go](https://www.wearedevelopers.com/videos/191-concurrency-with-go) (from "Concurrency with Go") - [Adding strong type checking and generic programming concepts](https://www.wearedevelopers.com/videos/526-the-design-and-evolution-of-c) (from "The Design and Evolution of C++") - [Abstracting variables through functional generics and explicit literal strings](https://www.wearedevelopers.com/videos/696-don-t-compromise-on-speedy-delivery-nor-type-safety-by-choosing-typescript) (from "Don't compromise on speedy delivery nor type-safety by choosing TypeScript") - [Navigating trait discovery and compiler error limitations](https://www.wearedevelopers.com/videos/847-eternal-sunshine-of-the-spotless-programming-language) (from "Eternal Sunshine of the Spotless Programming Language") - [Introduction to the speaker and software development philosophy](https://www.wearedevelopers.com/videos/100131-strategies-for-efficient-log-management-in-large-scale-kubernetes-clusters) (from "Strategies for Efficient Log Management in Large-Scale Kubernetes Clusters") ## Related Articles - [Using Java 17 latest features in real world projects](https://www.wearedevelopers.com/magazine/121-using-java-17-latest-features-in-real-world-projects) - [MLOps And AI Driven Development](https://www.wearedevelopers.com/magazine/82-mlops-and-ai-driven-development) - [Modern Java Switch Expressions](https://www.wearedevelopers.com/magazine/40-modern-java-switch-expressions) - [What is TypeScript?](https://www.wearedevelopers.com/magazine/47-what-is-typescript) ## Related Jobs - [Senior Software Engineer](https://www.wearedevelopers.com/jobs/ext/15942-senior-software-engineer) at **GitHub** - [AI Software Engineer (Germany)](https://www.wearedevelopers.com/jobs/48317-ai-software-engineer-germany) at **Sunhat** - [Golang Software Engineer](https://www.wearedevelopers.com/jobs/ext/1652471-golang-software-engineer) at **Redis** - [Software Engineer, Identity](https://www.wearedevelopers.com/jobs/ext/1713561-software-engineer-identity) at **Twilio** - [Mid/Senior Full-Stack Engineer (Web-first)](https://www.wearedevelopers.com/jobs/ext/1210833-mid-senior-full-stack-engineer-web-first) at **SMG Swiss Marketplace Group** - [Principal Software Engineer, Identity](https://www.wearedevelopers.com/jobs/ext/1469181-principal-software-engineer-identity) at **GitHub**