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

Solve golang error: non-interface type cannot be assigned to interface, solution

WBOY
Release: 2023-08-21 13:27:22
Original
925 people have browsed it

解决golang报错:non-interface type cannot be assigned to interface,解决方法

Solution to golang error: non-interface type cannot be assigned to interface, solution

In the process of using golang for development, sometimes you will encounter an error: non-interface type cannot be assigned to interface. This error usually occurs when assigning a variable of a non-interface type to an interface type.

The reason for this problem is that golang is a statically typed language, which requires that when performing type conversion, the converted type must satisfy all method signatures of the target type. Variables of non-interface types do not have method signatures, so they cannot be directly assigned to interface types.

Here are some ways to solve this error:

1. Type assertion

Type assertion is a technology that converts interface type variables into specific types, which can solve The problem of assigning values ​​from non-interface types to interface types. By using type assertions, we can convert non-interface types to target interface types.

The following is a simple example:

func main() {
    var str interface{}
    str = "Hello World"

    if s, ok := str.(string); ok {
        fmt.Println(s)
        // 输出:Hello World
    } else {
        fmt.Println("类型断言失败")
    }
}
Copy after login

In the above example, we assign a string type variable to an interface type variable through type assertion. The syntax for type assertion is variable.(type), where variable is the variable to be asserted and type is the target type. If the assertion succeeds, then s is a variable of the specific type, and the methods of the specific type can be accessed through s.

2. Interface nesting

If we need to assign a non-interface type variable to an interface type, and the non-interface type variable satisfies all method signatures of the interface type, then we can use the interface Nested methods implement assignment indirectly.

The following is an example:

type Writer interface {
    Write([]byte) (int, error)
}

type MyWriter struct{}

func (w MyWriter) Write(p []byte) (int, error) {
    // 自定义写入逻辑
    return len(p), nil
}

func main() {
    var writer Writer
    writer = MyWriter{}
}
Copy after login

In the above example, we define a Writer interface and a MyWriter structure, MyWriter The structure satisfies the method signature of the Writer interface. Then by assigning the MyWriter structure to a Writer type variable, the non-interface type is assigned to the interface type.

It should be noted that interface nested methods can only be used when the non-interface type satisfies all method signatures of the interface type. Otherwise, you still need to use type assertion.

3. Variable type conversion

If we know for sure that non-interface type variables can be converted to the target interface type, we can use the variable type conversion method to implement assignment.

The example is as follows:

type Reader interface {
    Read([]byte) (int, error)
}

type MyReader struct{}

func main() {
    var reader Reader
    reader = MyReader{} // 这里会报错

    reader = MyReader{}.(Reader) // 正确写法
}
Copy after login

In the above example, we define a Reader interface and a MyReader structure, MyReader The structure satisfies the method signature of the Reader interface. Then we try to assign the MyReader structure directly to a variable of type Reader, which will cause an error. The correct approach is to use variable type conversion to convert the MyReader structure to the Reader type.

Summary:

Non-interface types cannot be directly assigned to interface types. This is because non-interface types do not have method signatures, while interface types require variables to have method signatures. We can solve this problem through methods such as type assertion, interface nesting and variable type conversion.

In actual development, we should choose the appropriate solution based on specific needs and code specifications. If the non-interface type variable itself satisfies the method signature of the target interface type, then direct assignment is the optimal solution; if the non-interface type variable cannot be directly assigned to the interface type, then you can consider using type assertions or interface nesting to solve the problem. .

The above is the detailed content of Solve golang error: non-interface type cannot be assigned to 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