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:
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("类型断言失败") } }
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
.
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{} }
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.
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) // 正确写法 }
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!