Skip to content

Latest commit

 

History

History
101 lines (59 loc) · 1.61 KB

File metadata and controls

101 lines (59 loc) · 1.61 KB

Generics

Define the type in the function signature

Code in square brackets [T any] defines the type that the func argument will use (thing T)

func AnyExample[T any](thing T) {
	fmt.Println(thing)
}

MyPrint("hello world")
MyPrint(42)
MyPrint(true)

Constraints in types

Constrain the func argument to either int or string.

func ContstraintsExample[T int | string](intOrString T) {
	fmt.Println(intOrString)
}
func MultiTypeExample[A any, B int | string](a A, b B) {
	fmt.Printf("a: %+v, b: %+v", a, b)
}

Constraints with interfaces

Constrain with an interface that must implement a method. [T Stringer] defines an interface type that needs to satisfy Stringer

type Stringer interface {
	String() string
}

type myStringer struct{}
func (p myStringer) String() string {
	return "hello"
}

func StringerExample[T Stringer](thing T) {
	fmt.Println(thing.String())
}

Comparables

If a type can constrain to interfaces that need to define the functions on the interface, so as operators. This is what Go calls comparables. Comparable defines the == and != operators on the type.

func NotComparableExample[T any](a, b T) {
	fmt.Println(a == b)
}

NotComparableExample(1, 1) // invalid operation: a == b (incomparable types in type set)
func ComparableExample[T comparable](a, b T) {
	fmt.Println(a == b)
}

ComparableExample(1, 1) // true
ComparableExample(1, 2) // false

Refs