Python metaclasses change class creation. The closest Go themes are generics (compile-time type parameters) and reflect (runtime type inspection).
func MapKeys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
comparable is required for map keys and ==. Constraints can be interfaces listing method sets or unions of types.
stack[T], set[T] with map[T]struct{}Min, Contains constrained appropriatelyimport "reflect"
func describe(v any) {
rv := reflect.ValueOf(v)
rt := rv.Type()
_ = rt.Kind()
}
Reflection is verbose, loses compile-time checks, and can be slower — use for encoding (encoding/json), dependency injection frameworks, or generic serializers.
For repetitive boilerplate without generics, go generate with tools like stringer is idiomatic.
If types are known at compile time, normal functions and interfaces are clearer and safer.