Golang is a programming language similar to C and Java, but its concise syntax and fast compilation speed make it an excellent choice for developing high-performance network services. In Golang, the use of interfaces is very common because it allows applications to achieve flexible design and extension through interfaces. But sometimes we need to convert one interface to another interface, what should we do at this time? This article will introduce the methods and precautions for interface conversion in Golang.
The interface type in Go language is a very special type, which can represent the behavior of an object rather than the value of the object. Therefore, the value of an interface type is a tuple containing the actual value and the type of the value. In Golang, interface conversion is the process of converting a value of one interface type to a value of another interface type, or converting a value of an interface type to a value of a specific type.
Interface conversion in Golang is implemented through type assertions. Type assertions can convert a value of an interface type into a value of a specific type. Using type assertions, we can convert a value of an interface type to a value of another interface type, or we can convert a value of an interface type to a value of a concrete type. The basic syntax of interface conversion is as follows:
var variable_name interface{} var_new_name := variable_name.(new_interface_type)
In the above syntax, we declare an interface variable named variable_name
and convert it to an interface variable named The new interface type of var_new_name
. Type assertions use parentheses to enclose the interface type, followed by a dot and the new interface type, expressed as an expression.
The first example of interface conversion is to convert the value of the interface type to the value of the concrete type. Consider the following code:
func main() { var x interface{} x = 10 f(x) } func f(x interface{}) { i := x.(int) fmt.Printf("Value of i is %d\n", i) }
In the above code, we have defined an interface variable x
and assigned 10
to it. Then we pass x
to a function named f
. In the f
function, we first convert the interface type variable x
to the int
type and assign it to the variable i
. Finally, we print the value of i
.
When we run the above code, we will get the following output:
Value of i is 10
The above code demonstrates how to convert the interface type variable into a concrete type variable, which is very important for us in the program Useful when you need to use concrete types.
The second example of interface conversion is to convert a value of an interface type to a value of a different interface type. Consider the following code:
type Person interface { getName() string getAge() int } type Employee struct { name string age int id int } func (e *Employee) getName() string { return e.name } func (e *Employee) getAge() int { return e.age } func main() { var p Person e := &Employee{name: "John", age: 30, id: 1} p = e f(p) } func f(p Person) { fmt.Println(p.getName(), p.getAge()) e := p.(*Employee) fmt.Println(e.id) }
In the above code, we have defined an interface Person
and structure Employee
. Then we assign the instance of type Employee
to the variable p
of type Person
. In the function named f
, we first call the getName
and getAge
methods through p
and print the results. Then we convert p
to *Employee
type and assign it to the variable e
, and finally we print the id# of
e ##value.
John 30 1
The above is the detailed content of Let's talk about the methods and precautions for interface conversion in Golang. For more information, please follow other related articles on the PHP Chinese website!