Skip to content

Latest commit

 

History

History

type-parameters

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Type Parameters

Type Parameters in Functions

A Go function can be declared as follows:

func FunctionName(arg1 Type, arg2 Type, ...) {}

With Go generics, type parameters can now be applied to function declaration. Type parameters needs to be encapsulated in the square brackets [...] and before the function parameters.

func FunctionName[T1 TypeConstraint, T2 TypeContraint, ...](arg1 T, arg2 T,...) {}

Type Parameters in Types

With go 1.19, types can now be declared with type parameters.

type StructName[T TypeConstraint, ...] struct {
	attribute1 T
}

Reference