paint-brush
A Practical Guide to Interfaces in Golang by@mfbaseeksai
1,460 reads
1,460 reads

A Practical Guide to Interfaces in Golang

by Mohamed Fares Ben AyedSeptember 26th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

An interface is a set of method signatures that a type can implement. The interface defines the behaviors of an object (any kind of object) in Go. Go has static typing, which means that any variable in a program's life cycle has only one type (built-in or custom) Unlike Python, a variable can change its type only by reassigning it with a new value. Go can imitate the dynamic typing of Python and can change the variable from one type to another in a single program.
featured image - A Practical Guide to Interfaces in Golang
Mohamed Fares Ben Ayed HackerNoon profile picture


What made me interested in the Go programming language is the concept behind its interfaces. Object-oriented programming is conceived differently in Go, if we compare Go OOP to other OOP languages, we will notice a different approach to how objects behave in this language.


Go is famous for its simple and readable syntax, it is a language that contains only 25 built-in keywords. For example, a famous keyword like the class keyword (exists in most languages) doesn't exist in the syntax of Go. Instead, the struct is an alternative keyword for the class definition.


Because of this simplicity, concepts like inheritance, polymorphism, and abstraction are implemented differently in Go.


Before starting to explain interfaces, an object definition in Go is of type struct. so in order to define a Person struct in Go, we describe it in the following block of code:


type Person struct{
    Name string
    Age int
}


The person has 2 attributes (Name and Age attributes).


What is an interface?

An interface is a set of method signatures that a type can implement. therefore, the interface defines the behaviors of the object (any kind of object).


The definition is as simple as that.


For example, let's say we have an interface called Speak, it is defined as the following:


type Speak interface{
    Arabic()
    French()
    English()
}


To make a type implement the Speak interface, it (object or variable) must define all of the 3 methods implemented abstractly in the Speak interface : Arabic(), French() and English().


The Person type implements Speak, if itself has the 3 methods definitions:


func (p Person) Arabic(){ /* code here */ }
func (p Person) French(){ /* code here */ }
func (p Person) English(){ /* code here */ }


In other languages, you need to inherit from the interface using the keyword implements. But you just need to define the functions.


The capability of an empty interface

Go programming language has static typing, which means that any variable in a program's life cycle has to have only one type (built-in or custom). An integer variable remains as an integer variable, the same thing applies to the other types.


Unlike Python, a variable can change its type only by reassigning it with a new different value.Yet, Go can imitate the dynamic typing of Python and can change the variable from one type to another in a single program, and we can thank empty interfaces for that.


Let's see how we can’t change the types in this example:


func main(){
    var x int
    x = 5
    x = "change x"
    fmt.Println(x)
}


We reassigned the integer value x by a variable with a different type (a string "change x"), the program then displayed the following error:


./prog.go:10:6: cannot use "change x" (untyped string constant) as int value in assignment


However, if we change the type of x from int to an empty interface interface{}, the program will run successfully and the variable will be reassigned again.


func main() {
	var x interface{}
	x = 5
	fmt.Printf("x=%v of type :%T\n", x, x)
	x = "Fares"
	fmt.Printf("x=%v of type :%T", x, x)
}


The type of x changed dynamically from integer to string, and we can easily reassign it with any existing type in Go.


x=5 of type :int
x=Fares of type :string


Now, if we declare an empty interface variable without assigning it, what can be the result?

it will be a nil value.


Polymorphism using interfaces

if you want to do polymorphism in other languages, you need to make a parent class and multiple children classes inherited from the parent. then you declare an array of the parent class type and instantiate the subclasses and add them to the array.


In GO, things go differently, we can deal with an interface as a global custom type. we can build polymorphism on the example above and try this code block:


type Speaker interface{
    Arabic()
    English()
    French()
}
type NativeSpeaker struct{
    Name string
}
type ForeignSpeaker struct{
    Name string
    Nationality string
}
// methods for NativeSpeaker struct
func (NS NativeSpeaker) Arabic(){ /* code here */ }
func (NS NativeSpeaker) English(){ /* code here */ }
func (NS NativeSpeaker) French(){ /* code here */ }
// methods for ForeingSpeaker struct 
func (FS ForeignSpeaker) Arabic(){ /* code here */ }
func (FS ForeignSpeaker) English(){ /* code here */ }
func (FS ForeignSpeaker) French(){ /* code here */ }
// all 3 methods must be defined in order to belong to the Speaker type.
func main() {
    listOfSpeakers := make([]Speaker, 0)
    Sp1 := NativeSpeaker{Name: "John"}
    Sp2 := ForeignSpeaker{Name: "Mohamed"}
    listOfSpeakers = append(listOfSpeakers, Sp1, Sp2)
    fmt.Println(listOfSpeakers)
}


As you can see the Speaker interface is the global type that assembles NativeSpeaker and ForeignSpeaker custom types. those shares the same method signatures that were declared in Speak interface.

바카라사이트 바카라사이트 온라인바카라