Sobes.tech
Junior — Middle

How to determine if a specific interface is implemented using structures in a programming language?

sobes.tech AI

Answer from AI

In Go, an interface is implemented by a struct if the struct has all the methods defined in the interface. The implementation of the interface does not require explicit declaration — it is enough that the methods match by signature.

To determine that a specific interface is implemented by a struct, you need to check:

  • The struct has all the methods of the interface.
  • The signatures of these methods match.

Example:

package main

import "fmt"

type Speaker interface {
    Speak() string
}

type Person struct {
    Name string
}

func (p Person) Speak() string {
    return "Hello, my name is " + p.Name
}

func main() {
    var s Speaker = Person{Name: "Ivan"}
    fmt.Println(s.Speak())
}

Here, the Person struct implements the Speaker interface because it has a Speak method with the required signature.