1. Since the empty interface can store any type of value, can the values obtained from the empty interface be used directly? Look at the chestnut below
package main import ( "fmt" ) var a interface{} var b interface{} func main() { a = 1024 b = 100 res := a + b (res) }
Report an error:
invalid operation: operator + not defined on a (variable of type interface{}) (exit status 2)
Reason for program error: Because the type of an empty interface cannot be used directly, it is necessary to warn type assertion conversion before it can be used.
2. This time we use type assertions to convert the interface type to int type
package main import ( "fmt" ) var a interface{} var b interface{} func main() { a = 1024 b = 100 val1, res1 := a.(int) (val1, res1) val2, res2 := b.(int) (val2, res2) //val1 and val2 receive the converted values, res1 and res2 are the state of the type assertion (success or failure). Assertion success is true, otherwise false}
Output:
1024 true
100 true
3. New posture of type assertion: When using a value to accept the assertion result, the value after the assertion will be directly returned.
package main import ( "fmt" ) var a interface{} var b interface{} func main() { a = 1024 b = 100 //Type assertion conversion a1 := a.(int) b1 := b.(int) //Add to add it after conversion, there will be no errors res := a1 + b1 (res) }
4. Experience the pleasure of failing to use type assertion conversion
package main import ( "fmt" "log" ) var a interface{} func main() { a = 1024 if a1, r := a.(string); r { (a1) } else { ("Type assertion conversion failed") } }
Output:
2022/10/25 10:30:48 Type assertion conversion failed
The value stored in variable a is an integer. The view uses type assertion to convert it into a string. The result is an error. This is not possible and cannot be played.
5. Type assertion + switch implements data type judgment
package main import ( "fmt" ) func TestFunc(value interface{}) { switch value.(type) { case int: ("value=%v Type Int\n", value) case float32: ("value=%v Type Float32\n", value) case float64: ("value=%v Type Float64\n", value) case string: ("value=%v Type string\n", value) } } func main() { TestFunc("hello") TestFunc(100) TestFunc(89.12) }
Output:
value=hello Type string
value=100 Type Int
value=89.12 Type Float64
6. You can also convert the interface type to another interface type. The following chestnut is to convert interface A to interface B.
package main import ( "fmt" ) type A interface{} type B interface{} func main() { var a A = 100 b := a.(B) (b) }
In the previous six, the interface type was converted into basic data type, and this six were converted into a custom interface type to another custom interface type.
7. You can also convert the interface type to pointer type, see the following
package main import "fmt" func main() { //Define the variable ainter for interface type var ainter interface{} num := 100 ainter = &num // Assign the address to the interface variable v, r := ainter.(*int) (v, r) }
In the above chestnut, the interface type is converted to an int pointer type using type assertion.
8. Can interfaces be nested? Tell you the actual combat
package main import "fmt" type action1 interface { insert() } type action2 interface { delete() } type actionInterface interface { action1 action2 query() } type Db struct { Data string } func (d Db) insert() { ("Insert data...", ) } func (d Db) delete() { ("Delete data...", ) } func (d Db) query() { ("Query data...", ) } func main() { d := Db{Data: "hello"} () () () }
Through the above practical combat, interfaces can be nested. Note that only by implementing all methods in the interface (including all methods in all nested interfaces) can the interface be truly implemented.
9. Implement the Error method in the error interface to play a custom error type of xuzi
package main import ( "fmt" ) type AddError struct { ErrorMsg string } func (m AddError) Error() string { return ("Add error %v", ) } func add(a int, b int) (int, error) { if a == 0 || b == 0 { errinfo := ("a=%v, b=%v", a, b) return 0, AddError{ErrorMsg: errinfo} } else { return a + b, nil } } func main() { res, err := add(8, 0) (res, err) }
In the above chestnut, the Error method in the error interface has been implicitly implemented.
10. If you do not play with the custom error type, you can also use the method to return an error message directly.
package main import ( "errors" "fmt" ) func add(a int, b int) (int, error) { if a == 0 || b == 0 { return 0, ("Cannot be 0") } else { return a + b, nil } } func main() { res, err := add(9, 1) (res, err) }
This is the article about ten examples that will introduce you to the interfaces in Go. For more related Go language interface content, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!