Interfaces

 Interfaces in Golang

What is Interface 

In general, an interface is a set of methods or behaviors that a type must implement in order to be considered an implementation of that interface. Interfaces are a way of specifying a contract that a type must adhere to in order to be used in a certain context.

Interfaces are a common feature in many programming languages, and they are often used to define the behavior of types in a way that is decoupled from their implementation. This allows you to write code that is more flexible and easier to extend, as you can use different implementations of an interface without having to change the code that uses the interface.

For example, you might define an interface called Shape that defines methods for calculating the area and perimeter of a shape. You could then define multiple types that implement the Shape interface, such as Rectangle, Circle, and Triangle. This would allow you to write code that works with any type that implements the Shape interface, without having to know the specifics of how each type is implemented.

In general, interfaces are an important tool for designing and organizing code in a way that is flexible, reusable, and easy to maintain.

Interface in golang

In Golang an interface is a type that defines a set of methods that a type must implement to be considered an implementation of that interface. 
Interfaces allow you to define the behavior of a type in a flexible way, as a type can implement multiple interfaces.


In this example, the Printer interface defines a single method called Print, which takes no arguments and returns a string. The User struct is an implementation of the Printer interface because it has a method called Print with the correct signature. In the main function, we declare a variable p of type Printer and assign it a value of u, which is a pointer to a User struct. We can then call the Print method on p, which will call the implementation of Print in the User struct.

Interfaces are a powerful feature in Go that allow you to write code that is flexible and easy to extend.

They are often used to define the behavior of types in Go's standard library, as well as in third-party libraries and frameworks.

Example of an interface in Go : 


This interface defines two methods, Read and Write , that take a slice of bytes as an
argument and return the number of bytes read or written and an error value. Any
type that implements these two methods can be said to satisfy the ReadWrite interface.

To implement an interface, a type must provide an implementation for all the
methods defined in the interface. For example:

In this example, the File type satisfies the ReadWrite interface because it
implements both the Read and Write methods.
You can use an interface value to call the methods defined in the interface. For
example:

In this example, the variable rw has the type ReadWrite and is set to a value of type
*File . The Read method of the File type can be called through the rw variable
because *File satisfies the ReadWrite interface.

Another example:

In this example, we define the Shape interface with two methods: Area and Perimeter. We then define the Rectangle and Circle types, which implement the Shape interface by defining methods with the correct signatures.

In the main function, we create a slice of Shape values and add a Rectangle and a Circle to the slice. We can then iterate over the slice and call the Area and Perimeter methods on each element, without having to know the specifics of how each type is implemented.

This example demonstrates how interfaces can be used to write code that is flexible and easy to extend, as you can add new types that implement the Shape interface without having to change the code that uses the interface.

Golang interface vs java interface

Go interfaces and Java interfaces are similar in that they both allow you to define a
set of methods that a type must implement in order to satisfy the interface. However,
there are some key differences between the two:

1. Inheritance vs. explicit implementation: In Java, a type can implement an interface by inheriting from a base class that implements that interface. In Go, a type must explicitly declare that it implements an interface by providing implementations for the methods defined in that interface.

2. Go interfaces can contain any number of methods, while Java interfaces can
contain only a fixed number of methods: In Go, an interface can contain any
number of methods, including zero. In Java, an interface must contain at least
one method, and it can contain any number of methods up to a maximum of 64.

3. Go interfaces can be used as fields, while Java interfaces cannot: In Go, an
interface can be used as a field in a struct or as an element in an array. In Java,
an interface cannot be used as a field in a class.

4. Go interfaces do not have static or default methods, while Java interfaces do: In
Java, interfaces can have static and default methods, which are methods that
have an implementation and can be called directly on the interface. In Go,
interfaces do not have static or default methods, and all methods in an interface
must be implemented by any type that satisfies the interface.

Interview question

1.What is interface
2.What is Interface in Golang? 
3.Go interface vs java interface 

Comments

Popular posts from this blog

Golang Interview Questions Part 1 Theory Top 50 Question

Complete Golang Development setup on Linux (Ubuntu 20.04 )

Best GitHub repositories for Go