Go course

GenericsWhen NOT to Use Generics

Go

Display

Theme

Generics · Code example

When NOT to Use Generics

Generics are a powerful tool, but Go is designed to keep code simple and readable. Generics add a layer of abstraction, so use them only when the benefit is clear.

Signs that generics fit

  • You're writing exactly the same code for several types, and the only thing that changes is the type: functions over slices or maps (Filter, Unik, Keys), or general-purpose data structures (Stack, Antrean, Cache).
  • The function doesn't care about the element type; it only moves, stores, or compares values.

Signs you DON'T need generics

1. A regular interface is enough. If the function only calls methods, just accept an interface:

// Berlebihan:
func Cetak[T fmt.Stringer](v T) { fmt.Println(v.String()) }

// Lebih sederhana dan sama kuatnya:
func Cetak(v fmt.Stringer) { fmt.Println(v.String()) }

(The first version is overkill; the second is simpler and just as powerful.)

2. There's only one type. Don't write a generic function "just in case" when it's only ever called with int today. Write the concrete version; switch to generics when a second use actually shows up.

3. The behavior differs per type. If the function body branches on type (switch any(v).(type)), that's a sign you really need several different functions, or an interface with a method.

4. The signature gets hard to read. func Proses[K comparable, V any, S ~[]V, M ~map[K]S](...) might be technically correct, but your teammates will have to read it several times.

Rule of thumb: write concrete code first. When you find yourself copying the same function for a second or third type, that's when generics are worth considering.

Try it yourself

The example in the editor shows two approaches side by side: a generic Keys function (a good fit, since it works for any map) and an Umumkan function that just accepts an interface. Run it with Run, then try adding a new type that satisfies the Pengumuman interface. Notice that you don't have to change Umumkan at all.

main.go
package main

import (
	"fmt"
	"slices"
)

// Keys cocok dibuat generik: logikanya sama untuk map apa pun.
func Keys[K comparable, V any](m map[K]V) []K {
	hasil := make([]K, 0, len(m))
	for k := range m {
		hasil = append(hasil, k)
	}
	return hasil
}

// Pengumuman adalah interface biasa — tidak perlu generics.
type Pengumuman interface {
	Teks() string
}

type PesanSistem struct{ Isi string }

func (p PesanSistem) Teks() string { return "[SISTEM] " + p.Isi }

type PesanPengguna struct{ Nama, Isi string }

func (p PesanPengguna) Teks() string { return p.Nama + ": " + p.Isi }

// Umumkan cukup menerima interface.
func Umumkan(daftar []Pengumuman) {
	for _, p := range daftar {
		fmt.Println(p.Teks())
	}
}

func main() {
	stok := map[string]int{"kopi": 12, "teh": 30, "coklat": 5}
	nama := Keys(stok)
	slices.Sort(nama) // urutan map acak, jadi diurutkan dulu
	fmt.Println("Menu:", nama)

	poin := map[int]bool{3: true, 1: true, 2: true}
	id := Keys(poin)
	slices.Sort(id)
	fmt.Println("ID:", id)

	Umumkan([]Pengumuman{
		PesanSistem{"Server akan restart pukul 23.00"},
		PesanPengguna{"Rina", "Siap, terima kasih infonya!"},
	})
}

Output

Go runtime not loaded yet

Press Run to see what the program prints.

Ctrl/Cmd + Enter: Run · Ctrl/Cmd + S: tidy the code (gofmt)