Giter Club home page Giter Club logo

go-generics-the-hard-way's Introduction

Go generics the hard way

I started using Go back around 2015 and was immediately surprised by the lack of a generic type system. Sure, the empty interface{} existed, but that was hardly the same. At first I thought I wanted needed generics in Go, but over time I began appreciating the simplicity of the language. Therefore I was ambivalent at best when I learned of discussions to introduce generics in Go 2.0, and once the timetable was accelerated to 1.18, I decided it was time to dig into the proposal.

After a while, I gained an appreciation for how generics are implemented with the same elegance as Golang itself, and this moved me to share my experience. Go generics the hard way is a culmination of the time I spent playing with this new feature and provides a hands-on approach to learning all about generics in Go.

  • Labs: a hands-on approach to learning Go generics
  • FAQ: answers to some of the most frequently asked questions regarding Go generics
  • Links: links to related reference material and projects that use generics

Labs

  1. Prerequisites: how to install the prerequisites required to run the examples in this repository
  2. Hello world: a simple example using generics
  3. Getting started: an introduction to go generics
  4. Getting going: basic concepts explored
  5. Internals: how generics are implemented in golang
  6. Benchmarks: basic benchmarks for common patterns using generics
  7. Lessons learned: lessons learned from digging into generics

FAQ

How are you using generics in the Go playground?

We can use the Go playground in “Go dev branch” mode to edit and run your program with generics.

What is T?

The symbol T is often used when discussing generic types because T is the first letter of the word type. That is really all there is too it. Just like x or i are often the go-to variable names for loops, T is the go-to symbol for generic types.

For what is worth, K is often used when there is more than one generic type, ex. T, K.

What is this any I keep seeing everywhere?

The word any is a new, predeclared identifier and is equivalent to the empty interface in all ways. Simply put, writing and reading any is just more user friendly than interface{} 😃.

What does the tilde ~ do?

The ~ symbol is used to express that T may be satisfied by a defined or named type directly or by a type definition that has the same, underlying type as another defined or named type. To learn more about type constraints and the ~ symbol, please refer to the section Tilde ~.

Do Go generics use type erasure?

Generics in Go are not implemented with type erasure. Please jump to Internals for more information.

Links

Additional reading

Projects using generics

  • Controller-runtime: a write-up and patchset for implementing conditions logic, patch helpers, and simple reconcilers using generics
  • Go collections: generic utility functions for dealing with collections in go
  • go-generics-example: examples using generics

go-generics-the-hard-way's People

Contributors

733amir avatar aaomidi avatar akutz avatar alexisomg avatar brackendawson avatar budougumi0617 avatar chrisbirster avatar dzonint avatar fsmiamoto avatar mattn avatar pippolo84 avatar sg0hsmt avatar sleeping-barber avatar zdyxry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-generics-the-hard-way's Issues

Reword expression in `06-careful-constructors`?

Would you agree that this should be reworded from address to a ... to pointer to a ...? I was expecting to see the address when reading this line whereas the code example and point you make is about a pointer type.

Hmm, that's odd, an _address_ to a `UniqueName` was printed, not `UniqueName`? That's because that is what was provided as `T` -- `*UniqueName`. And not just that, but what happens if the value at that address is printed ([Go playground](https://gotipplay.golang.org/p/bvAi8Ax0Q9f)):

Inconsistent syntax?

As a middle aged hobby coder just starting out with Go I appreciate the simple and straight forward syntax (I’m middle management by day .. and I get the tingling feeling there’s a joke in there somewhere😄). I also enjoy to read the (so far) consistent reasoning of the maintainers for choices made.

Do you have any idea why this (https://github.com/akutz/go-generics-the-hard-way/blob/490784b7d7c082ea84ddeadefcb02c9b03ff7a5a/03-getting-started/03-constraints.md) logical OR in the generic constraints wasn’t two pipes too? Or is the subtle difference that this generic constraint statement doesn’t actually “result” in a Boolean value or something? Something like why variable assignment and function parameters (signature) have the type the other way around.

I love stuff like this, partly, I’m sure, because if it makes logical sense it sticks too. Not really an issue with this repo of course (which was timely, complete and well thought out!).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.