liveBook · Manning

Type-safe collection

As explained in Go in Practice, Second Edition

A type-safe collection is a collection whose operations preserve the declared element type without requiring unchecked runtime type conversions.

A type-safe collection preserves its declared element type without unchecked runtime conversions. Code generation creates type-specific collections, such as queues, while avoiding type assertions, runtime safety problems, and their performance cost. section 13.3

Generating a MyInt queue

A //go:generate ./queue MyInt directive invokes the generator. Running go generate creates myint_queue.go; the program then constructs NewMyIntQueue, inserts three MyInt values, and removes 1 first.

How Go in Practice, Second Edition covers it

How deep it goes
Explains queue operations, templates, generated files, and generation directives, but stops short of broader collection designs.
Examples
Demonstrates generated MyInt queues, FIFO insertion and removal, and go generate output.
What you can do after
Build it from scratch by writing a queue template, generator, generation directive, and generated type-specific queue.
How it connects
Connects type-safe collections with code generation, queues, Go templates, generics, and type assertions.

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.