Golang cannot use generic type without instantiation


I’m trying to define a generic data structure using generics, particularly a rudimentary HashTable, I try the following:

type hashable interface {

type Node[K hashable] struct {
    Key   K
    Value any

type HashTable[K hashable] struct {
    size  int
    table [][]Node

But I get the following error on the line table [][]Node

cannot use generic type Node[K hashable] without instantiation

I’m not instantiating anything, just defining, what does it want me to do?


Node is a parameterized type, so when you want to use it, you have to instantiate its type parameters.

The type you use to instantiate it can of course be a type parameter of the enclosing type, so simply do it like this:

type HashTable[K hashable] struct {
    size  int
    table [][]Node[K]

Answered By – icza

Answer Checked By – Jay B. (GoLangFix Admin)

Leave a Reply

Your email address will not be published.