1.

How are interfaces implemented in Golang?

Answer»

Golang language interfaces are DIFFERENT from other LANGUAGES. In Golang, type implements an interface through the implementation of its METHODS. There are no explicit declarations or no “implements” keyword.

Example

package main
import "fmt"
type I interface {
   M()
}
type T STRUCT {
   S STRING
}
// This method means type T implements the interface I,
// but we don't need to explicitly declare that it does so.
func (t T) M() {
   fmt.Println(t.S)
}
func main() {
  var i I = T{"hello"}
  i.M()
}



Discussion

No Comment Found