Go interface example

In this example, we show how to use the interface in Golang with an example.

An interface is a set of function signatures and it is a specific type. Another type implements an interface by implementing its functions. While languages like Java or C# explicitly implement interfaces, there is no explicit declaration of intent in Go.

Go interface example

The following example uses a simple Shape interface.

package main

import (

type Shape interface {
    Area() float64

type Rectangle struct {
    Width, Height float64

type Circle struct {
    Radius float64

func (r Rectangle) Area() float64 {
    return r.Width * r.Height

func (c Circle) Area() float64 {
    return math.Pi * c.Radius * c.Radius

func getArea(shape Shape) {


func main() {

    r := Rectangle{Width: 7, Height: 8}
    c := Circle{Radius: 5}




Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course