The Golang interface is an interesting construct, and most people from programming languages like Java will find it different. If you are new to Golang, this post helps you get your head around the Golang interface with examples.
Golang Interface in Simpler Terms
The Golang interface is an advanced topic and may require newbies to understand basic programming examples with Golang. For example, we need to know how to declare and initialize variables or structs. Then, learn how to create functions for data types in Golang. In simpler terms, the Golang interface is a construct that defines a set of functions with unique return types and names. Consider the following sample codes that define an interface.
1 2 3 4 5 | type Mammal interface { getType() string canFly() bool feed(foodCount int) string } |
The example Golang Mammal interface represents any mammals like cats, dogs, or people. Notice the function names, parameter lists, and return types. How do we implement an interface in Golang? Any data type in Golang can declare its own set of functions. For instance, we have the following codes that define a function for the type MyType using the built-in string data type.
1 2 3 4 5 | type MyType string func (MyType) myFunction(param string) string { return param } |
Implementing a Golang interface means defining the functions for a type with similar function signatures defined in the interface. Therefore, we define all the functions for a type, e.g., struct or string, to implement the Mammal interface. In case we miss one or more functions in the definition, that type fails to implement the Golang interface. Consider the following example wherein the type Person correctly implements the Mammal Golang interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 | type Person string func (Person) getType() string { return "Human" } func (Person) canFly() bool { return false } func (Person) feed(foodCount int) string { return "Ate " + strconv.Itoa(foodCount) + " unit of food" } |
Verify If Interface Implementation Works
How do we verify if our example codes correctly implement the Golang Mammal interface? Suppose we have the following function that accepts a parameter of Mammal type.
1 2 3 4 5 | func printMammal(b Mammal) { fmt.Println("Type:", b.getType()) fmt.Println("Can it fly?", b.canFly()) fmt.Println("Ate:", b.feed(10)) } |
Since our example Person type correctly implements the Mammal interface, we can pass an instance of the Person type to the printMammal function.
1 2 3 4 | func main() { karl := Person("Karl") printMammal(karl) } |
When we run the main function, we get the following output.
1 2 3 | Type: Human Can it fly? false Ate: Ate 10 unit of food |
The Complete Golang Interface Example
The complete Golang example codes are as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package main import ( "fmt" "strconv" ) type Mammal interface { getType() string canFly() bool feed(foodCount int) string } type Person string func (Person) getType() string { return "Human" } func (Person) canFly() bool { return false } func (Person) feed(foodCount int) string { return "Ate " + strconv.Itoa(foodCount) + " unit of food" } func main() { karl := Person("Karl") printMammal(karl) } func printMammal(b Mammal) { fmt.Println("Type:", b.getType()) fmt.Println("Can it fly?", b.canFly()) fmt.Println("Ate:", b.feed(10)) } |
Have you finally understood Golang interfaces?