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.