I am trying to create a function using Go Generics that accepts any structs that meet minimum struct criteria. Something like <T extends {name: string, age: number}> in Typescript.
Here below is my function and I am trying to find a way to make it work. I would love to be able to pass any struct that has Name string and Age int to the printNameAndAge function because that function will be using only these two fields. Is this possible? I was not able to find an example or documentation for this use case?
package main
import "fmt"
type User struct {
Name string
Age int
Height float32
}
type Car struct {
Age int
Name string
Model string
}
type NameAndAge struct {
Name string
Age int
}
func printNameAndAge[T (**missing extends or something like that**) NameAndAge](u T) {
fmt.Println(u.Name)
fmt.Println(u.Age)
}
func main() {
newUser := User{
Name: "John",
Age: 24,
Height: 186,
}
newCar := Car{
Name: "Series 3",
Age: 1,
Model: "BMW",
}
printNameAndAge(newUser)
printNameAndAge(newCar)
}