Junior — Middle
How to indicate in Go that a structure implements a certain interface?
sobes.tech AI
Answer from AI
In Go, a struct implicitly implements an interface if it contains all the methods declared in that interface. There is no need to explicitly declare that a struct implements an interface — it is enough to implement the corresponding methods.
Example:
package main
import "fmt"
type Speaker interface {
Speak() string
}
type Dog struct {}
func (d Dog) Speak() string {
return "Woof!"
}
func main() {
var s Speaker = Dog{} // Dog implements the Speaker interface
fmt.Println(s.Speak())
}
Here, Dog implements the Speak() method, so it automatically implements the Speaker interface.