Failed type assertion, return value is zero value of the most recent assertion type
The code is entered:
func main() { var data interface{} = "ehoo" if res, ok := data.(int); ok { ("int res:%d\n", res) } else if res, ok := data.(bool); ok { ("bool res:%b\n", res) } else { ("other res:%v\n", res) // Assert the zero value of the type, the result is: false } }
Supplement: Examples of go language interface{} type assertions
In go language, interface{} type is often used, which is similar to the void * type in c language and can accept any type of parameters. When this parameter is present in our function or method, we must judge the specific type value and then process it accordingly.
General format: return value, bool variable:=interface{} variable. (the specific type to be judged), so that it is asserted by a specific type, and the return value is of this specific type.
For example: v,b:=a.(string), this is to assume that the interface{} variable a is of type string, and the return value of v is a variable of type string. As for whether the assumption is successful, it depends on whether b is true or false.
In this way, if you want to judge multiple types, you need to write multiple statements separately, v, b:=a.(string), v1, b1:=a.(int), v2, b2:=a.(float64)... Because the return values of each time v, v1, v2 are different prophetic types values, it is a bit troublesome.
Using a statement like switch .(type) case will save much more trouble. The format is: return value:=interface{} variable.(type) case Specific type: case Specific type: ......
type Student struct { m_Addr string m_ID int } func (stu *Student) SetAddr(addr interface{}) (bool, error) { //This way of writing: return value, bool variable: =interface{} variable. (the specific type to be judged), which is to determine whether it is a value of the specified type. v, b := addr.(string) //This is to assert that v is a variable of type string and can only be assigned to values of the same type. if b { //If the string type to be judged above will return true here stu.m_Addr = v return true, nil } else { //If it is not the string type of judgment, it will return false, and the following will return the error message return false, ("Not a string type") } } func (stu *Student) SetStu(data ...interface{}) (bool, error) { for _, d := range data { //Loop through the parameter list and ignore the first parameter index /*This writing method is to judge one by one type. It is more troublesome when judging multiple types. It is much easier to use the switch case below. var v, b = d.(string) if b { stu.m_Addr = v } var v1, b1 = d.(int) if b1 { stu.m_ID = v1 } */ switch v := d.(type) { // Pass type type, which can only be used in the switch case statement, and the returned v is the specific value. case int: //Judge the specific type in each case and process the return value v stu.m_ID = v case string: stu.m_Addr = v } } return true, nil }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.