1. 程式人生 > 其它 >go 的介面轉化為型別,斷言

go 的介面轉化為型別,斷言

package main

import (
	"fmt"
)

func test(a interface{}) {
	// s := a.(int)   // a.(int)介面轉為int 如果不是這個型別,那就會panic,加入ok接收異常,就可以避免panic
	s, ok := a.(int)
	if ok {
		fmt.Println(s)
		return
	}
	str, ok := a.(string)
	if ok {
		fmt.Println(str)
		return
	}

	f, ok := a.(float32)
	if ok {
		fmt.Println(f)
		return
	}
	fmt.Println("can not define the type of a")
}

func testInterface1() {
	var a int = 100
	test(a)

	var b string = "hello"
	test(b)
}

func testSwitch(a interface{}) {
	switch a.(type) {
	case string:
		fmt.Printf("a is string, value:%v\n", a.(string))
	case int:
		fmt.Printf("a is int, value:%v\n", a.(int))
	case int32:
		fmt.Printf("a is int, value:%v\n", a.(int32))
	default:
		fmt.Println("not support type\n")
	}
}
// 這種斷言方式最好,比testSwitch少了一次轉化
func testSwitch2(a interface{}) {
	switch v := a.(type) {
	case string:
		fmt.Printf("a is string, value:%v\n", v)
	case int:
		fmt.Printf("a is int, value:%v\n", v)
	case int32:
		fmt.Printf("a is int, value:%v\n", v)
	default:
		fmt.Println("not support type\n")
	}
}

func testInterface2() {
	var a int = 100
	testSwitch(a)
	var a1 int32 = 100
	testSwitch(a1)
	var b string = "hello"
	testSwitch(b)
}

func testInterface3() {
	var a int = 100
	testSwitch2(a)
	var b string = "hello"
	testSwitch2(b)
}

func main() {
	//testInterface1()
	//testInterface2()
	testInterface3()
}