Interface-empty interface
1. What is an empty interface?
Empty interfaces are special forms of interface types. Ordinary interfaces have methods, while empty interfaces do not define any method ports. Therefore, we can say that all types implement at least empty interfaces.
type empty_iface interface { }
Each interface contains two properties, one is a value and the other is a type.
For empty interfaces, both are nil, you can use fmt to verify
package main import "fmt" type empty_interface interface{ } func main() { var e empty_interface (e) ("type: %T, value: %v", e, e) }T, value: %v", i, i) }
The output is as follows
<nil>
type: <nil>, value: <nil>
2. How to use an empty interface?
First,
Usually we will use it directlyinterface{}
An instance is declared as a type, and this instance can carry arbitrary type of values.
package main import ( "fmt" ) func main() { // Declare an empty interface instance var i interface{} // There is no problem with saving int i = 1 ("type: %T, value: %v\n", i, i) // There is no problem with storing strings i = "hello" ("type: %T, value: %v\n", i, i) // There is no problem with storing boolean values i = false ("type: %T, value: %v\n", i, i) }
Output result:
type: int, value: 1
type: string, value: hello
type: bool, value: false
second,
If you want your function to receive any type of value, you can also use an empty interface
Receive a value of any type Example
package main import ( "fmt" ) func myfunc(iface interface{}){ (iface) } func main() { a := 10 b := "hello" c := true myfunc(a) myfunc(b) myfunc(c) }
Output result:
type: int, value: 10
type: string, value: hello
type: bool, value: true
Receive any value of any type Example
package main import ( "fmt" ) func myfunc(ifaces ...interface{}){ for _,iface := range ifaces{ ("type: %T, value: %v\n", iface, iface) } } func main() { a := 10 b := "hello" c := true myfunc(a, b, c) }
Output result:
type: int, value: 10
type: string, value: hello
type: bool, value: true
third,
You also define an array, slice, map, and strcut that can receive any type, for example, define a slice here
package main import "fmt" func main() { any := make([]interface{}, 5) any[0] = 11 any[1] = "hello world" any[2] = []int{11, 22, 33, 44} for i, value := range any { ("arr[%d]:\nType: %T, Value: %v\n", i,value, value) } }
Output result:
arr[0]:
Type: int, Value: 11
arr[1]:
Type: string, Value: hello world
arr[2]:
Type: []int, Value: [11 22 33 44]
arr[3]:
Type: <nil>, Value: <nil>
arr[4]:
Type: <nil>, Value: <nil>
3. Several pitfalls to pay attention to in empty interfaces
Pit 1:
The empty interface can carry any value, but does not mean that any type can bear the value of the empty interface type.
From an implementation point of view, any type of value satisfies an empty interface. Therefore, the empty interface type can save any value, or the original value can be taken out from the empty interface.
But if you assign an object of an empty interface type to an object of a fixed type (such as int, string, etc.), an error will be reported.
package main func main() { // Declare a variable, type int, initial value is 1 var a int = 1 // Declare the i variable, type is interface{}, the initial value is a, and the value of i becomes 1 at this time var i interface{} = a // Declare the b variable, try to assign i var b int = i }
This error is like something that can be placed in a gift box, which can definitely be placed in a container, but on the other hand, something that can be placed in a container may not be placed in a gift box, and this reverse operation is directly prohibited in Go. (Statement: There must be other reasons for the underlying principle, but for novices, this explanation may be easier to understand.)
Object-oriented\empty interface\:11:17: cannot use i (variable of type interface{}) as int value in variable declaration: need type assertion
Pit 2:
When the empty interface carries an array and slice, the object (that is an empty interface object) can no longer be sliced
package main import "fmt" func main() { sli := []int{2, 3, 5, 7, 11, 13} // Empty interface bears slice var i interface{} i = sli // Use slices to generate slices g := i[1:3] (g) }
An error will be reported if execution is performed.
Object-oriented\empty interface\:11:10: invalid operation: cannot slice i (variable of type interface{})
Pit 3:
When you use an empty interface to receive parameters of any type, its static type is interface{}, but we don't know about the dynamic type (int, string or other types), so we need to use type assertions.
package main import ( "fmt" ) func myfunc(i interface{}) { switch x:=i.(type) { case int: ("The value of the parameter is: %v, the type of the parameter is int\n",x) case string: ("The value of the parameter is: %v, the type of the parameter is string\n",x) } } func main() { a := 10 b := "hello" myfunc(a) myfunc(b) }
The output is as follows
The value of the parameter is: 10, the type of the parameter is int
The value of the parameter is: hello, the type of the parameter is string
This is the end of this article about the specific use of go hollow interface. For more related go empty interface content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!