Introduce in detail how to use .() type assertion in golang


Golang is a very popular programming language that is widely used in many fields. In the development process, many times we need to pass functions as parameters to other functions. At this time, we need to use the .() usage in golang. This article will introduce in detail how to use .() in golang.

1. What is the .() usage?

In golang, .() is called a type assertion, which can convert a variable of type interface{} into other types, such as string type, integer type, etc. When calling a function, we can use .() to convert function arguments to different types.

2. Basic usage of .()

The basic syntax for .() usage is as follows:

value, ok := interface{
    
    }.(Type)

Among them, value is the converted value, and ok is a return value of bool type, indicating whether the conversion is successful.

The following is a simple example showing how to use .() to convert a variable of type interface{} to a string type:

func main() {
    
    
    var i interface{
    
    } = "hello"
    s := i.(string)
    fmt.Println(s)
}

The output is:

hello

3. Advanced application of .() usage

.() is very flexible to use and can play an important role in different scenarios of the program. Below are two high-level application scenarios.

3.1 Conversion of nil type

The nil type is very common in golang, which can represent pointer types, slice types, and more. In the process of type conversion, we need to pay attention to the situation of handling nil type. In this case, we need to use the ok value to determine whether the conversion was successful.

Here's an example that demonstrates how to handle the ok value during conversion to the nil type:

func main() {
    
    
    var i interface{
    
    } = nil
    _, ok := i.(string)
    fmt.Println(ok) // false
}

The output is:

false

3.2 Passing functions as parameters

In golang, functions can be passed as parameters to other functions. .() is useful if we need to convert a function parameter (i.e. a parameter to a function call) to a different type when calling the function.

Here is an example that demonstrates how to use .() to convert a function argument to an integer type:

func main() {
    
    
    f := func(i int) {
    
    
        fmt.Println(i)
    }
    test(f, "2")
}

func test(f func(int), s interface{
    
    }) {
    
    
    if i, ok := s.(int); ok {
    
    
        f(i)
    }
}

The output is:

2

In this example, we pass the integer type "2" as a function parameter to the function test, and use .() in the function test to convert it to int type so that it can be passed to the function f.
insert image description here

4. Precautions in the use of .()

When using .(), you need to pay attention to the following points:
If we try to convert a variable of non-pointer type into a pointer type, then .() will cause panic;
if we try to convert a variable of non-slice type into a slice type, then .() will panic;
therefore, you need to be very careful when using .(), follow programming best practices, and test and inspect your code as much as possible.

V. Summary

To sum up, the .() usage in golang is a very useful feature that allows us to convert function parameters into different types when the function is called. In actual development, we can use .() to handle different scenarios according to different needs.

Guess you like

Origin blog.csdn.net/weixin_44816664/article/details/132128136