Home > Article > Backend Development > Solve golang error: non-interface type cannot be used as type interface, solution
Solution to golang error: non-interface type cannot be used as type interface, solution
In the process of programming in Go language, we often encounter various problems kind of error. One of the common errors is "non-interface type cannot be used as type interface". This error commonly occurs when we try to assign a non-interface type to an interface type. Next, we’ll explore the causes of this error and how to fix it.
Let’s first look at an example where this error occurs:
type Printer interface { Print() } type MyStruct struct { Name string } func (m MyStruct) Print() { fmt.Println(m.Name) } func main() { var printer Printer myStruct := MyStruct{Name: "John Doe"} printer = myStruct printer.Print() }
In the above example, we defined an interface Printer
, which has a method Print()
. Then, we defined a structure MyStruct
and implemented the Print()
method for it. Then, we tried to assign a variable of type MyStruct
to a variable of type Printer
printer
. Finally, we call the Print()
method of printer
.
When we try to compile this code, we will encounter an error: "cannot use myStruct (type MyStruct) as type Printer in assignment: MyStruct does not implement Printer (missing Print method)". The meaning of this error is that the MyStruct
type does not implement the Print()
method in the Printer
interface.
Observing the error message, we can see that the MyStruct
type does not implement the Print()
method of the Printer
interface. That's why the error occurs.
To resolve this error, we need to ensure that our type implements all methods in the interface. In our example, the MyStruct
type should implement the Print()
method of the Printer
interface. In order to fix the code, we only need to change the Print()
method of MyStruct
to pass the pointer type:
func (m *MyStruct) Print() { fmt.Println(m.Name) }
After modifying the code, we will not run the program again Compilation errors occurred again.
In order to better understand the problem, we can also look at a more complex example:
type Shape interface { Area() float64 } type Rectangle struct { Width float64 Height float64 } func (r *Rectangle) Area() float64 { return r.Width * r.Height } func CalculateArea(s Shape) { area := s.Area() fmt.Println("The area is:", area) } func main() { rect := Rectangle{Width: 5, Height: 10} CalculateArea(rect) }
In this example, we define an interface Shape
, which has A methodArea()
. Then, we defined a Rectangle
structure and implemented the Area()
method for it. Next, we define a function CalculateArea()
, which accepts a parameter that implements the Shape
interface and calculates the area of the shape. Finally, we create a Rectangle
type variable rect
in the main()
function and pass it to CalculateArea()
function.
When we try to compile this code, we will encounter the error again: "cannot use rect (type Rectangle) as type Shape in argument to CalculateArea". The reason for this error is that we tried to assign a variable of type Rectangle
to a parameter of type Shape
.
To resolve this error, we can fix the code by changing the type of rect
to a pointer type:
rect := &Rectangle{Width: 5, Height: 10}
In this way, we can change the ## of the pointer type #rect is passed to the
CalculateArea() function.
The above is the detailed content of Solve golang error: non-interface type cannot be used as type interface, solution. For more information, please follow other related articles on the PHP Chinese website!