gogenericsgo-playground

go2go.playground - expected type, found 'type' (and 1 more errors)


I try to run examples from the design draft (The Next Step for Generics) on go2go.playground

type Pair(type T) struct { f1, f2 T }

, but get an error

prog.go2:14:11: expected type, found 'type' (and 1 more errors)

Where can I find actual go generics design draft?


Solution

  • That's old syntax since a few weeks ago. Try

    type Pair[T any] struct { f1, f2 T }
    

    Note that square brackets are now used instead of round brackets and the type keyword is no longer used. You also must use the any constraint, whereas previously you could leave the constraint out if there was no restriction on the type parameter.

    BTW Conventionally Pair refers to a struct with 2 different types for the 2 fields like type Pair[T1, T2 any] struct { first T1; second T2 }

    See go2go Playground for example code that builds.

    As mentioned in the first paragraph of the 2019 design draft the new draft is https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md