Go programming example [Type conversion supported by Go]

The first: explicit type conversion

var a int8 = 5
b := int16(a)

The second type: implicit type conversion handled by the compiler

//Data类型隐式转换成interface{}类型
type Data struct {
    
    
}

func demo(v interface{
    
    }) {
    
    
}

func main() {
    
    
    demo(Data{
    
    })
}

The third type: type assertion

type Data struct {
    
    
}

func demo2(d Data) {
    
    
}


func demo1(v interface{
    
    }) {
    
    
    //断言v为Data类型
    demo2(v.(Data))
}

func main() {
    
    
    //Data类型隐式转换成interface{}类型
    demo1(Data{
    
    })
}

Guess you like

Origin blog.csdn.net/weiguang102/article/details/130468368