August 2019
Beginner to intermediate
798 pages
17h 2m
English
This subsection will teach you how to use the interface defined in myInterface.go in a Go program named useInterface.go, which will be presented in five parts.
The first part of useInterface.go comes with the next Go code:
package main
import (
"fmt"
"math"
"myInterface"
)
type square struct {
X float64
}
type circle struct {
R float64
}
As the desired interface is defined in its own package, it should come as no surprise that you are importing the myInterface package.
The second code portion from useInterface.go contains the following code:
func (s square) Area() float64 {
return s.X * s.X
}
func (s square) Perimeter() float64 {
return 4 * s.X
}
In this part, you implement the shape interface for the square type.
Read now
Unlock full access