Golang type assertion and type conversion
1. Type assertion
-
Type assertions are used to check whether the value held by the interface type variable implements the desired interface or specific type.
-
The syntax of type assertions is as follows:
PrimaryExpression.(Type)
- PrimaryExpression must be a variable of interface type, which can be empty or non empty.
- Type represents the type to be judged (interface, specific type)
1.1 judgment type
-
Judge whether the object implementing the interface is a type.
-
Format:
value, ok := pri.(T) // pri represents the variable to be judged // T represents the judged type // Value represents the returned value // ok means whether it is the same type
-
Example (example of empty interface)
-
Judge whether the empty interface b variable stores the value of string type
var B interface{} b := B b = "Eswin" str, ok := b.(string) if ok { fmt.Println(str) }
1.2 interface assertion
-
Like type assertions, interface assertions in go language are all methods used to determine whether an object has an interface
-
Knowledge related to method set:
-
When the receiver of a method is defined as a value type, the Go language compiler will automatically convert it. Therefore, the receiver of a value type and the receiver of a pointer type are equivalent. No error will be reported during compilation, and the corresponding method can be called during operation.
-
When the receiver type is a pointer and any one of the defined or asserted types is a value, an error will be reported during compilation, prompting that the echo method should be a pointer type receiver.
-
When the receiver type is a value, it is also a value when defining the structure. When the assertion type is a pointer, there will be no problem with compilation, but exceptions will be thrown during operation.
-
When implementing the interface, the receiver definition, structure definition and assertion type shall be consistent.
-
-
Format:
value ,ok := Obj.(Ti) // Obj represents the specific object (interface variable) to be judged // Ti indicates the specific interface // When the object has the interface method we want to judge, the value of ok is true, and the specific method can be called by returning the value value
-
example
package main import ( "fmt" ) type Flyer interface { Fly() } type Walker interface { Walk() } type Bird struct { } type Dog struct { } func (b Bird) Fly() { fmt.Println("the bird can fly!") } func (b Bird) Walk() { fmt.Println("the bird can walk!") } func (d *Dog) Walk() { //Because the pointer is used here, when calling the interface method, the address must be passed, not the value. fmt.Println("the dog can walk!") } func main() { var i interface{} //Declare an interface object i = new(Bird) //Create a Bird structure variable, return a structure pointer, and assign it to the interface v, b := i.(Flyer) //Determine whether there is a flight interface, if b { //If the judgment is successful, v is the flight interface type v.Fly() } v1, b := i.(Walker) //Determine whether there is a traveling interface if b { v1.Walk() } //Create a Dog structure variable and assign the structure address to the interface. If it is written as i=Dog {}, the Walk method cannot be called because there is a structure pointer received. //Therefore, when assigning a specific type variable to the interface, try to use pointer transfer, that is, new (structure type) or & structure type {}, //In this way, no matter whether the interface method implementation of type variables is passed by value or pointer, it can be called. i = &Dog{} v, b = i.(Flyer) if b { v.Fly() } v1, b = i.(Walker) if b { v1.Walk() } } //output // the bird can fly! // the bird can walk! // the dog can walk!
- Note: the above Dog type Walk method is passed by pointer, so when assigning value to the interface, it should be passed by address, that is, the sentence I = & Dog {}. If it is written as i=Dog {}, "the dog can walk" will not be printed.
2. Type conversion
-
Type conversion is used to convert a variable of one type to a variable of another type.
-
Methods: cast types or use interface conversions (assertions, Section 1).
-
Cast basic format
type_name(expression)
- type_name is type
- Expression is an expression.
-
example
package main import "fmt" func main() { var sum int = 17 var count int = 5 mean := float32(sum) / float32(count) fmt.Printf("mean The value of is: %f\n", mean) }
Reference connection: Go language type conversion