ProgramGuru

Generic Methods

Start with Generic Methods

Go 1.27 introduced methods with their own type parameters. This extends earlier support for methods on generic receiver types.

What you’ll learn

  • Explain the main purpose of generic methods.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Since Go 1.18, a method of a generic type may use its receiver’s parameters, as in func (b Box[T]) Get() T. Beginning with Go 1.27 (August 2026), a method declaration can introduce an additional type parameter: func (b Box[T]) Convert[U any](f func(T) U) U.

Generic methods cannot implement interface methods, and interface method declarations themselves cannot declare type parameters. The primary runnable example below demonstrates receiver type parameters and is compatible with Go 1.23; the Go 1.27 example below demonstrates the new syntax.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for generic methodsA method can now introduce a type parameter beyond its receiver type.Box[T] receiverStep 1Go 1.27 method [U]Step 2Pass mapper T -> UStep 3Return UStep 4A method can now introduce a type parameter beyond its receiver type.
A method can now introduce a type parameter beyond its receiver type.

Tools, syntax and practical notes

Go 1.27+: a true generic method

The following complete program requires the Go 1.27 language version. It is provided separately because the validation environment uses Go 1.23.

package main

import "fmt"

type Box[T any] struct { Value T }

func (b Box[T]) Convert[U any](f func(T) U) U {
    return f(b.Value)
}

func main() {
    box := Box[int]{Value: 12}
    text := box.Convert(func(n int) string {
        return fmt.Sprintf("ID-%d", n)
    })
    fmt.Println(text)
}

Expected output: ID-12. See Go 1.27 release notes and Generic Methods for the language rules. Ordinary receiver-parameter methods still work with older Go releases.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

type Box[T any] struct{ Value T }

func (b Box[T]) Get() T   { return b.Value }
func (b *Box[T]) Set(v T) { b.Value = v }
func main() {
	number := Box[int]{Value: 4}
	number.Set(9)
	fmt.Println("Value:", number.Get())
}

Output

Value: 9

Trace what the program does

The pointer-receiver Set updates the Box[int] value from 4 to 9, and Get returns an int. This is an established generic receiver method, not the new Go 1.27 method-level parameter feature shown below.

Avoid common mistakes

  • A method-level type parameter needs Go 1.27 or newer, not Go 1.23.
  • A generic method is not a way to satisfy an interface method with its own type parameters.

Try it yourself

With Go 1.27, add a Convert[U any] method that maps Box[int] into a string. Compare it with a standalone generic Convert function.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

When did Go add methods that declare their own new type parameters?
Choose one answer. You can try again.

Answer and explanation

Go 1.27

Correct. The pointer-receiver Set updates the Box[int] value from 4 to 9, and Get returns an int.

What you should remember

You have practiced Generic Methods in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Iterators →