Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. interface

interface

An interface defines a set of methods that a type must implement. In Go, interfaces are implemented implicitly — no explicit declaration is required.

Syntax

// Define an interface.
type InterfaceName interface {
    MethodName(params) ReturnType
}

// The empty interface (any) accepts all types.
var v any = "Hello"
var v interface{} = 42

Interface overview

Syntax / TypeDescription
type I interface { M() }Defines an interface that requires the method M().
anyA built-in alias for interface{} (Go 1.18+). Accepts all types.
interface{}An empty interface with zero methods. Every type satisfies it.
Type assertion: v.(T)Converts an interface value to a concrete type. Panics if the conversion fails.
Type assertion: v, ok := v.(T)A safe form of type assertion. If the conversion fails, ok is false and no panic occurs.
Type switch: switch v.(type)Branches execution based on the concrete type of an interface value.

Sample code

package main

import (
    "fmt"
    "math"
)

// Define the Shape interface.
type Shape interface {
    Area() float64
    Perimeter() float64
}

// Define a Circle struct.
type Circle struct {
    Radius float64
}

// Circle implicitly implements the Shape interface.
func (c Circle) Area() float64 {
    return math.Pi * c.Radius * c.Radius
}

func (c Circle) Perimeter() float64 {
    return 2 * math.Pi * c.Radius
}

// Define a Rectangle struct.
type Rectangle struct {
    Width, Height float64
}

// Rectangle also implements the Shape interface.
func (r Rectangle) Area() float64 {
    return r.Width * r.Height
}

func (r Rectangle) Perimeter() float64 {
    return 2 * (r.Width + r.Height)
}

// A function that accepts the Shape interface as a parameter.
func printShape(s Shape) {
    fmt.Printf("Area: %.2f, Perimeter: %.2f\n", s.Area(), s.Perimeter())
}

func main() {
    c := Circle{Radius: 5}
    r := Rectangle{Width: 3, Height: 4}

    printShape(c) // Circle satisfies Shape.
    printShape(r) // Rectangle also satisfies Shape.

    // The empty interface accepts all types.
    var anything any = "hello"
    fmt.Println(anything) // Prints "hello".
    anything = 123
    fmt.Println(anything) // Prints "123".

    // Use a type assertion to convert to a concrete type.
    var s Shape = Circle{Radius: 3}
    if circle, ok := s.(Circle); ok {
        fmt.Println("Radius:", circle.Radius) // Prints "Radius: 3".
    }

    // Use a type switch to branch by type.
    values := []any{42, "hello", true, 3.14}
    for _, v := range values {
        switch val := v.(type) {
        case int:
            fmt.Printf("int: %d\n", val)
        case string:
            fmt.Printf("string: %s\n", val)
        default:
            fmt.Printf("other: %v\n", val)
        }
    }
}

Notes

Unlike many other languages, Go does not require a type to explicitly declare that it implements an interface. If a type implements all the methods an interface requires, it satisfies that interface automatically. This is known as "duck typing."

The type assertion v.(T) panics if the conversion fails. To convert safely, use the two-value form v, ok := v.(T).

The empty interface any (formerly interface{}) can hold any type, but you must use a type assertion or type switch to work with the underlying value. Where possible, define a specific interface rather than relying on any to preserve type safety.

If you find any errors or copyright issues, please .