空接口和类型断言
This commit is contained in:
parent
2e69f9483a
commit
320ca10ac9
28
bilibili/aceid/struct/kong/test.go
Normal file
28
bilibili/aceid/struct/kong/test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func myFunc(arg interface{}) {
|
||||
fmt.Println("myFunc is called...")
|
||||
fmt.Println(arg)
|
||||
// interface{}的类型断言
|
||||
value, ok := arg.(string)
|
||||
if !ok {
|
||||
fmt.Println("arg is not string")
|
||||
} else {
|
||||
fmt.Println("arg is type, value = ", value)
|
||||
fmt.Printf("valut type is %T\n", value)
|
||||
}
|
||||
}
|
||||
|
||||
type Book struct {
|
||||
auth string
|
||||
}
|
||||
|
||||
func main() {
|
||||
book := Book{"Golang"}
|
||||
myFunc(book)
|
||||
myFunc(100)
|
||||
myFunc("abc")
|
||||
myFunc(3.14)
|
||||
}
|
Loading…
Reference in New Issue
Block a user