mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
39 lines
664 B
Go
39 lines
664 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
var i = 5
|
|
var str = "ABC"
|
|
|
|
type Person struct {
|
|
name string
|
|
age int
|
|
}
|
|
|
|
type Any interface{}
|
|
|
|
func main() {
|
|
var val Any
|
|
val = 5
|
|
fmt.Printf("val has the value: %v\n", val)
|
|
val = str
|
|
fmt.Printf("val has the value: %v\n", val)
|
|
pers1 := new(Person)
|
|
pers1.name = "Rob Pike"
|
|
pers1.age = 55
|
|
val = pers1
|
|
fmt.Printf("val has the value: %v\n", val)
|
|
switch t := val.(type) {
|
|
case int:
|
|
fmt.Printf("Type int %T\n", t)
|
|
case string:
|
|
fmt.Printf("Type string %T\n", t)
|
|
case bool:
|
|
fmt.Printf("Type boolean %T\n", t)
|
|
case *Person:
|
|
fmt.Printf("Type pointer to Person %T\n", *t)
|
|
default:
|
|
fmt.Printf("Unexpected type %T", t)
|
|
}
|
|
}
|