- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Function that takes an interface type as value and pointer in Golang
In Go, it is common to write functions that take interfaces as arguments or pointers to interfaces. This can be useful when you want to write generic code that works with any type that satisfies a particular interface.
Functions That Take an Interface Type as Value
In Go, interfaces are defined as a set of methods. If a type implements all the methods of an interface, then it is said to satisfy the interface. This means that the type can be used wherever the interface is expected.
Here is an example of a function that takes an interface type as value −
Example
package main import "fmt" type Shape interface { Area() float64 } type Circle struct { Radius float64 } func (c Circle) Area() float64 { return 3.14 * c.Radius * c.Radius } func PrintArea(s Shape) { fmt.Println("Area:", s.Area()) } func main() { c := Circle{Radius: 5} PrintArea(c) }
Output
Area: 78.5
In this example, we define an interface Shape with a single method Area() float64. We also define a Circle type that implements the Shape interface by defining the Area() method. Finally, we define a function PrintArea that takes a value of the Shape interface as its argument and prints its area.
In the main function, we create an instance of the Circle type and pass it as an argument to the PrintArea function. Since Circle satisfies the Shape interface, it can be used as an argument to the PrintArea function.
Functions That Take an Interface Type as Pointer
In Go, it is also common to write functions that take pointers to interfaces as arguments. This can be useful when you want to modify the state of the underlying value.
Here is an example of a function that takes a pointer to an interface −
Example
package main import "fmt" type Shape interface { Area() float64 } type Circle struct { Radius float64 } func (c Circle) Area() float64 { return 3.14 * c.Radius * c.Radius } func PrintArea(s Shape) { fmt.Println("Area:", s.Area()) } func main() { c := Circle{Radius: 5} s := Shape(c) PrintArea(s) }
Output
Area: 78.5
In this example, we define an interface Shape with a single method Area() float64. We also define a Circle type that implements the Shape interface by defining the Area() method.
We then define a function PrintArea that takes a pointer to a Shape interface as its argument and prints its area. In the main function, we create an instance of the Circle type and assign a pointer to it to a variable of type Shape. We then pass a pointer to this variable as an argument to the PrintArea function.
Note that when we define the PrintArea function, we use a pointer to the Shape interface as its argument. This allows us to modify the state of the underlying value. In this case, we do not actually modify the value, but we could if we wanted to.
Conclusion
Writing functions that take interfaces as arguments or pointers to interfaces is a powerful feature of Go that allows you to write generic code that works with any type that satisfies a particular interface.