Home >Backend Development >Golang >Solve golang error: invalid type assertion: 'x'.(T) (non-interface type U on left)
Solve golang error: invalid type assertion: 'x'.(T) (non-interface type U on left)
Foreword:
In Golang development, using type assertions is a common technique for converting an interface type value into a specified concrete type. However, when using type assertions, sometimes you will encounter an error: invalid type assertion: 'x'.(T) (non-interface type U on left). This article will detail the reasons for this error and provide solutions.
Problem background:
In Golang, the syntax of type assertion is very simple, usually written as: value.(Type), where value is a value of an interface type, and Type is a specific type . Type assertion is safe when value's underlying type is the same as Type or Type is an interface of value's underlying type. However, when the underlying type of Type is incompatible with value, the above error will occur.
Problem analysis:
First, let’s look at a simple example:
package main import "fmt" func main() { var x int = 10 y := x.(float64) fmt.Println(y) }
In the above code, the variable x is a value of type int. When using type assertion to convert x to float64 type, the above error will be triggered. Because the int type and float64 type are incompatible, this type assertion is invalid.
Solution:
For the above problems, we can solve it in the following ways:
In the above example, we are trying to convert a value of type int to type float64. To solve this problem, you can directly declare x as float64 type instead of int type. The modified code is as follows:
package main import "fmt" func main() { var x float64 = 10 y := x.(float64) fmt.Println(y) }
If we really need to convert a value to another type, we can use the type conversion operator to convert . The modified code is as follows:
package main import "fmt" func main() { var x int = 10 y := float64(x) fmt.Println(y) }
In the above example, float64(x) is used to convert the value x of type int to type float64.
If you cannot determine whether a type assertion will succeed, you can use ok mode to avoid reporting errors. An example is as follows:
package main import "fmt" func main() { var x interface{} = 10 if y, ok := x.(float64); ok { fmt.Println(y) } else { fmt.Println("无法转换为float64类型") } }
In the above example, we declare x as an empty interface type and use the if statement to determine whether the type assertion is successful. If successful, print the converted value; if failed, print a prompt message.
Summary:
In Golang, type assertions can be used to convert a value of an interface type into a specified specific type. However, when the underlying type is incompatible with the type to be converted, an error will be triggered: invalid type assertion: 'x'.(T) (non-interface type U on left). We can avoid this error by using the correct type, type conversion or type assertion in ok mode.
The above is the detailed content of Solve golang error: invalid type assertion: 'x'.(T) (non-interface type U on left). For more information, please follow other related articles on the PHP Chinese website!