0

I am using a third-party library that has two types:

type Foo struct {
  a bool
  b bool
}

type Bar struct {
  a bool
  b bool
  c bool
}

They have common fields (a and b), and Bar has the extra c. The third-party library doesn't implement any interface above them that I could use.

I want a setup function that creates these structs and sets some default values, but these values are the same for both types.

I tried the following code:

func SetupFooOrBar[T *Foo | *Bar](t T) {
  t.a = true
  t.b = false
  // if type is Bar, then we set c
}

It doesn't work: t.a undefined and t.b undefined.

Is there any way to develop this as DRY as possible?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Vitor Falcão
  • 1,007
  • 1
  • 7
  • 18

0 Answers0