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).
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.
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.
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.