Implementing an Interface
In order to implement an interface in the Go programming language, it is necessary to implement all of the methods declared in the interface. Interfaces in the Go language are implemented implicitly, without using any specific keyword.
Example
package main
import "fmt"
// Creating an interface
type square interface {
// Methods
area() int
perimeter() int
}
type val struct {
side int
}
// Implementing methods
func (m val) area() int {
// area of a square
return (m.side)*(m.side)
}
func (m val) perimeter() int {
// perimeter of a square
return 4*(m.side)
}
func main() {
//creating a variable of interface
var s square
s = val{10}
fmt.Println("Area of the square :", s.area())
fmt.Println("Perimeter of the square:", s.perimeter())
}
Output
Area of the square : 100
Perimeter of the square: 40
Empty Interface
If an interface contains zero methods, then such a type of interface is called an empty interface. All the types can implement the empty interface. An empty interface is represented by interface().
Example
package main
import "fmt"
func main() {
var i interface{}
describe(i)
i = "Hello World"
describe(i)
}
func describe(i interface{}) {
fmt.Printf("(%v, %T)\n", i, i)
}
Output
(<nil>, <nil>)
(Hello World, string)
Type Assertions
Type assertion is an operation that is applied to the value of the interface in the Go language. It is a process that is used to access the interface values underlying the concrete value.
Syntax
x.(T)
Here, x is the value of the interface, and T is the type (asserted type). We use type assertion to check whether the dynamic type of the operand matches with the assertion type or not. In this, if T is of concrete type, then the type assertion will check the given dynamic type of “x” is equal to T, if the checking proceeds successfully, then the type assertion will return the dynamic value of “x”, but if it fails, then the operation will panic. If T is of an interface type, then the type assertion will check the given dynamic type of “x” satisfies T, if checking proceeds successfully, then the dynamic value is not extracted.
Example
package main
import "fmt"
func print(x interface{}) {
// Extracting the value of x
val := x.(string)
fmt.Println("Value: ", val)
}
func main() {
var val interface { } = "Hello World"
print(val)
}
Output
Value: Hello World
If we change the val:=x.(string) statement into val:=x.(int), in the above code, then the program will panic. To overcome this problem, we use
val, ok:=x.(T)
If the type of “x” is equal to T, then ok will be set to true, and the value contains the dynamic value of “x”. But if the type of “x” is not equal to T, then ok will be set to false, and the value contains zero value, and the program doesn't panic.
Example
package main
import "fmt"
func print(x interface{}) {
value, ok := x.(int)
fmt.Println(value ,ok)
}
func main() {
var x1 interface {
} = 20
print(x1)
var x2 interface {
} = "Hello World"
print(x2)
}
Output
20 true
0 false
Type Switch
Type switch is used to compare the concrete type of an interface with the multiple types provided in the case statements. In type switch, the cases specifies the types, not the values.
Example
package main
import "fmt"
func print(x interface{}) {
// Using type switch
switch x.(type) {
case int:
fmt.Println("int Type, Value:", x.(int))
case string:
fmt.Println("\nstring Type, Value: ", x.(string))
case float64:
fmt.Println("\nfloat64 Type, Value: ", x.(float64))
default:
fmt.Println("\nType not found")
}
}
// Main method
func main() {
print(100)
print("Hello World")
print(false)
}
Output
int Type, Value: 100
string Type, Value: Hello World
Type not found
FAQs
-
What is an empty interface()?
An interface that contains zero methods is known as an empty interface().
-
What is the difference between type assertion and type switch?
Type assertion and type switch both are used to compare the concrete type of interface, the only difference is type assertion is used to compare values, whereas the type switch is used to compare the types.
-
What is the use of interface?
We can use interface in methods or functions to pass different types of arguments like Println() function. We can also use an interface when multiple types implement the same interface.
Key Takeaways
In this article, we have extensively discussed what interface is, how to declare an interface, what is type assertion and type switch with examples in the Go programming language, and their implementation in Visual Studio Code.
We hope that this blog has helped you enhance your knowledge regarding interface in the Golang programming language and if you would like to learn more, check out our articles on what golang is, what are the dynamic of Golang, understanding abstract classes in Java, what is the difference between classes and interfaces in Java.
Do upvote our blog to help other ninjas grow. Happy Coding!”