Skip to content
Search

Go:Generics

lastmod 2025-11-20created 2025-11-201 edits

Generic function

A generic function is a function that can use parameters of various types within the function. In the Go language, you can write a generic function in the following form.

func doSomething[T any](arg1 T, arg2 T) {
    // T 타입의 인자를 사용하여 함수 로직을 작성합니다.
}

In the example code above, [T any] is a generic type parameter. T represents the type used within the function, and the any keyword means that all types are allowed. All parameters of type T used within the function are treated as the same type.

Generic type

A generic type is a type that can perform the same behavior across multiple types. In the Go language, you can write a generic type in the following form.

type exampleType[T any] struct {
    // T 타입의 필드를 정의합니다.
}

In the example code above, [T any] is a generic type parameter. T represents the type used in that type, and the any keyword means that all types are allowed. Using generic types, you can perform the same behavior across multiple types while guaranteeing type safety.

See also

Favorite site

author Zer0 sitemap