Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement interface from a different package in golang?

Tags:

go

I am beginner in golang and was trying interfaces. I want to keep interfaces in a separate packages so that I can use it to implement this in various other packages, also provide it to other teams (.a file) so that they can implement custom plugins. Please see example below on what I would like to achieve.

--- Folder structure ---
gitlab.com/myproject/
                  interfaces/
                            shaper.go
                  shapes/
                        rectangle.go
                        circle.go

---- shaper.go ---
package interfaces

type Shaper interface{

    Area() int

}

How do I ensure that the rectangle.go implements shaper interface? I understand that go implements interfaces implicitly, does this mean rectangle.go automatically implements shaper.go even though it is in a different package?

I tried it like below, but when I run gofmt tool, it removes the import because it is unused.

--- rectangle.go ---
package shapes

import "gitlab.com/myproject/interfaces"

type rectangle struct{

  length int
  width int
}

func (r rectangle) Area() int {
 return r.length * r.width
}

Thanks in advance.

like image 658
Sanketh Avatar asked Jun 03 '17 17:06

Sanketh


People also ask

Can an interface implement another interface Golang?

As we know that the Go language does not support inheritance, but the Go interface fully supports embedding. In embedding, an interface can embed other interfaces or an interface can embed other interface's method signatures in it, the result of both is the same as shown in Example 1 and 2.

How is interface implemented in Golang?

To implement an interface in Go, you need to implement all the methods declared in the interface. Go Interfaces are implemented implicitly. Unlike Java, you don't need to explicitly specify using the implements keyword.

Can a struct implement multiple interfaces in Golang?

Note: In Go language, you are not allowed to create same name methods in two or more interfaces. If you try to do so, then your program will panic.

What does interface {} mean in Golang?

interface{} means you can put value of any type, including your own custom type. All types in Go satisfy an empty interface ( interface{} is an empty interface). In your example, Msg field can have value of any type.


1 Answers

There is an excellent section in the go wiki about interfaces:

Go interfaces generally belong in the package that uses values of the interface type, not the package that implements those values. The implementing package should return concrete (usually pointer or struct) types: that way, new methods can be added to implementations without requiring extensive refactoring.

This also has the advantage that it reduces coupling between packages (by not forcing anybody to import your package just for the interface) and it generally leads to smaller interfaces (by allowing people to consume only a subset of the interface that you would have built).

If you are new to go I highly recommend reading the "Go Code Review Comments" wiki article I linked and if you have some more time also Effective Go. Happy hacking!

like image 165
Friedrich Große Avatar answered Nov 01 '22 09:11

Friedrich Große