Type inference lets the Go compiler deduce a generic function's type argument from its input arguments, so callers can often omit explicit type arguments. section 7.1.1
A generic function declares a type parameter such as T and a constraint such as any. When T appears in an input parameter, the compiler determines its concrete type at each call. Inference does not use returned types. section 7.1.1
Omitting explicit type arguments
func prettyPrint[T any](t T) { fmt.Printf("> %v", t) } prettyPrint(.25) // T is inferred as float64 prettyPrint("pockets") // T is inferred as string section 7.1.1
func prettyPrint[T any](t T){
fmt.Printf("> %v", t)
}
prettyPrint(.25) #1
prettyPrint("pockets") #2
Compare the generic function declaration with calls that rely on compiler inference. section 7.1.1
Type inference has these boundaries:
- Infer type arguments from input parameters when the generic type appears there. section 7.1.1
- Write explicit square-bracket type arguments when inference cannot determine the desired type. section 7.1.1
- Do not expect inference to derive a type solely from a function's returned type. section 7.1.1















