Home > Backend Development > Golang > Solve golang error: non-interface type cannot be used as type interface, solution

Solve golang error: non-interface type cannot be used as type interface, solution

WBOY
Release: 2023-08-19 23:46:44
Original
1048 people have browsed it

解决golang报错:non-interface type cannot be used as type interface,解决方法

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()
}
Copy after login

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 Printerprinter. 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)
}
Copy after login

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)
}
Copy after login

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}
Copy after login

In this way, we can change the ## of the pointer type #rect is passed to the CalculateArea() function.

In this article, we introduce the solution to the error "non-interface type cannot be used as type interface" in golang. This error usually occurs when we try to assign a non-interface type to an interface type. We need to ensure that all non-interface types implement the methods in the corresponding interface. With the sample code in this article, we can better understand this error and know how to fix it.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template