visit
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.
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
}
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.
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
it will be a nil
value.
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.