liveBook · Manning

Kind switch

As explained in Go in Practice, Second Edition

A kind switch selects behavior based on the broad underlying category reported by Go reflection rather than on an exact named type.

A kind switch selects behavior by a reflected value’s broad category, allowing one branch to handle multiple named types with the same underlying kind. It suits generic functions that receive interface{} values, such as summing arbitrary numeric and string inputs. section 13.1.1

Named integer type

A type switch ignores MyInt because it is not exactly int64. A kind switch recognizes its underlying integer category, so sum(a, b, c, d) includes the MyInt value.

How Go in Practice, Second Edition covers it

How deep it goes
Descends through reflection setup, grouped kind cases, value extraction, and the limitation of exact type switches.
Examples
Uses a variadic sum function handling integers, unsigned integers, numeric strings, and a named integer type.
What it assumes
Assumes familiarity with interface{} and switches while introducing reflect.Value and reflect.Kind through a concrete function.
What you can do after
Build a reflection-based dispatcher with reflect.ValueOf, Kind, and typed extraction methods.
How it connects
Connects type switches, reflect.Type, reflect.Kind, reflection, and generic functions.

Related concepts

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.