Golang converts the interface to int, string, slice, struct and other types

In golang, interface {} allows any value, int, string, struct, slice, etc., so I can simply pass the value to interface {}

However, when we pass any type into the test function and convert it into an interface, we often need to perform a series of operations that the interface does not have (that is, the passed in User structure, and the interface itself does not have the so-called Name attribute). At this time, we need to use the interface properties type assertions and type switches to convert it back to the original passed in type

package main
import (
    "fmt"
)
type User struct{
    Name string
}
func main() {
    any := User{
        Name: "fidding",
    }
    test(any)
    any2 := "fidding"
    test(any2)
    any3 := int32(123)
    test(any3)
    any4 := int64(123)
    test(any4)
    any5 := []int{1, 2, 3, 4, 5}
    test(any5)
}
func test(value interface{}) {
    switch value.(type) {
    case string:
        // 将interface转为string字符串类型
        op, ok := value.(string)
        fmt.Println(op, ok)
    case int32:
        // 将interface转为int32类型
        op, ok := value.(int32)
        fmt.Println(op, ok)
    case int64:
        // 将interface转为int64类型
        op, ok := value.(int64)
        fmt.Println(op, ok)
    case User:
        // 将interface转为User struct类型,并使用其Name对象
        op, ok := value.(User)
        fmt.Println(op.Name, ok)
    case []int:
        // 将interface转为切片类型
        op := make([]int, 0)  //[]
        op = value.([]int)
        fmt.Println(op)
    default:
        fmt.Println("unknown")
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish