Canonical guidance

Use when

Avoid

Preferred pattern

func fieldNames(v any) []string {
	t := reflect.TypeOf(v)
	if t.Kind() == reflect.Pointer {
		t = t.Elem()
	}

	var names []string
	for i := 0; i < t.NumField(); i++ {
		names = append(names, t.Field(i).Name)
	}
	return names
}

Anti-pattern

Explanation: This anti-pattern is tempting because it feels generic, but it usually trades compile-time clarity for runtime fragility.

Why

Related pages

Sources