0
0
GoHow-ToBeginner · 4 min read

How to Create Interface in Go: Syntax and Examples

In Go, you create an interface using the type keyword followed by the interface name and the interface keyword with method signatures inside curly braces. Interfaces define behavior by specifying methods without implementing them, and any type that implements those methods satisfies the interface automatically.
📐

Syntax

An interface in Go is declared using the type keyword, followed by the interface name and the interface keyword. Inside the braces, you list method signatures that any type must implement to satisfy the interface.

  • type: declares a new type
  • InterfaceName: the name of your interface
  • interface: keyword to define an interface
  • Method signatures: method names with parameters and return types, but no body
go
type InterfaceName interface {
    Method1(param1 Type1) ReturnType1
    Method2(param2 Type2) ReturnType2
}
💻

Example

This example shows how to create an interface Speaker with one method Speak. Then a type Person implements this method. We create a function that accepts the interface and calls the method.

go
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 greet(s Speaker) {
    fmt.Println(s.Speak())
}

func main() {
    p := Person{Name: "Alice"}
    greet(p)
}
Output
Hello, my name is Alice
⚠️

Common Pitfalls

Common mistakes when working with interfaces in Go include:

  • Not implementing all methods of the interface in your type, so it does not satisfy the interface.
  • Using pointer receivers vs value receivers incorrectly, which can cause the interface not to be satisfied.
  • Trying to instantiate an interface directly, which is not possible because interfaces are abstract.
go
package main

import "fmt"

type Speaker interface {
    Speak() string
}

type Person struct {
    Name string
}

// Wrong: method with pointer receiver, but passing value type
func (p *Person) Speak() string {
    return "Hi, I'm " + p.Name
}

func greet(s Speaker) {
    fmt.Println(s.Speak())
}

func main() {
    p := Person{Name: "Bob"}
    // greet(p) // This will cause a compile error because Person does not implement Speaker
    greet(&p) // Correct: pass pointer to Person
}
📊

Quick Reference

  • Use type InterfaceName interface { ... } to declare an interface.
  • Any type that implements all interface methods satisfies the interface automatically.
  • Interfaces hold method signatures only, no implementations.
  • Use pointer receivers if methods modify the receiver or to satisfy interface with pointer types.
  • You cannot create instances of interfaces directly.

Key Takeaways

Declare interfaces with the type keyword and list method signatures inside interface braces.
Any type implementing all interface methods automatically satisfies the interface.
Pointer vs value receivers affect whether a type satisfies an interface; be consistent.
Interfaces cannot be instantiated directly; use concrete types that implement them.
Use interfaces to write flexible and reusable code by programming to behavior, not implementation.