- 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
How do you declare an interface in Golang?
Declaring an interface in go language means creating a new named type that defines a collection of method signatures. In go-language we can declare an interface using single method interface, multiple method interface as well as embedded interface. In this article we are going to understand these methods and declare an interface in go language with the help of various examples.
Method 1: Single Method Interface
The first method involves a single interface, in this approach we describe an interface that all implementing types must fulfill.
Algorithm
Create the interface with a single method called CreateSound().
Now, create the struct cat, that represents a cat.
Implement the CreateSound() function on the cat struct. It will print "meow!" to the console.
Now declare a variable of type Animal in the main function.
On the animal variable, use the CreateSound() function.
It outputs "meow!" to the console.
Example
This code defines an interface Animal and a struct Cat. The interface defines a single method named “Create Sound()” which prints the output and the cat struct implement the CreateSound() function.
package main import "fmt" type Animal interface { CreateSound() } type cat struct{} func (d cat) CreateSound() { fmt.Println("meow!") } func main() { var animal Animal animal = cat{} animal.CreateSound() // Output: Woof! }
Output
meow!
Method 2: Using Multiple Method Interface
This other method involves using multiple interfaces, in this method we are going to see go language interfaces that can include many methods.
Algorithm
Define the Shape interface using two methods Area() and Perimeter.
Now, Create a struct named Rectangle with type float64 length and width.
Implement the Area() function. Return the result of multiplying the length by the width.
Implement the Perimeter() function. Return the perimeter that is calculated using the formula = 2 * (length + width).
Assign a value of type Rectangle to the shape variable, with the length set to 8 and the width set to 5.
Print the result of the Area() function. Print the result of the Perimeter() function.
Example
The example given below demonstrates the use of interface in go language to calculate the area and perimeter of a rectangle.
package main import "fmt" type Shape interface { Area() float64 Perimeter() float64 } type Rectangle struct { length, width float64 } func (r Rectangle) Area() float64 { return r.length * r.width } func (r Rectangle) Perimeter() float64 { return 2 * (r.length + r.width) } func main() { var shape Shape shape = Rectangle{length: 8, width: 5} fmt.Println(shape.Area()) fmt.Println(shape.Perimeter()) }
Output
40 26
Method 3: Using Embedded Interfaces
This method involves embedding an interface inside another interface, allowing the new interface to adopt all the methods from the embedded interface.
Algorithm
Create an interface named Reader which has a Read() function that returns a []byte.
Create the interface named Writer and define the function Write(data []byte).
Define a struct file.
Implement the File struct's Read() function.
Implement the File struct's Write(data []byte) function.
In the main function, Declare a variable rw of type ReadWrite.
Assign the rw variable a value of type File, indicating that a File implements the ReadWrite interface.
Execute the Read() method on the rw variable and save the results.
Pass the data variable as a parameter to the Write(data) method on the rw variable.
Proram use the Read() function to read data from a file and the send() method to send data to the console.
Example
The example given below explains the notation of interface composition, which involves mixing multiple interfaces together to create a new interface.
package main import "fmt" type Reader interface { Read() []byte } type Writer interface { Write(data []byte) } type ReadWrite interface { Reader Writer } type File struct { // implementation details } func (f File) Read() []byte { return []byte("Read data from file") } func (f File) Write(data []byte) { fmt.Println("Write data to file:", string(data)) } func main() { var rw ReadWrite rw = File{} data := rw.Read() rw.Write(data) }
Output
Write data to file: Read data from file
Conclusion
In this article we have discussed how we can declare an interface using a single interface, multiple interface and embedded interface method. Interfaces in go language are very helpful tools to achieve polymorphism in the code and make the code flexible.