If I have these types:
type Orange struct {
Size float32
}
type Lemon struct {
Color string
}
type Wood struct {
Variety string
}
And that interface:
type Fruit interface {
}
How do I declare that Orange and Lemon are Fruit,
so that, elsewhere, a function may return only things who are of kind Fruit?
(Fruit being a marker interface).
>Solution :
To declare that a type belongs to a marker interface in Go, you need to explicitly state that the type implements the interface. In your case, to declare that Orange and Lemon types are of kind Fruit, you can do the following:
type Fruit interface {
}
type Orange struct {
Size float32
}
func (o Orange) MethodOfFruit() {
// Implement any methods of Fruit interface if required
}
type Lemon struct {
Color string
}
func (l Lemon) MethodOfFruit()
{
// Implement any methods of Fruit interface if required
}