首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 处理任意类型(空接口参数)
|
||||
func processAny(v interface{}) {
|
||||
// 类型断言:判断实际类型
|
||||
switch val := v.(type) {
|
||||
case int:
|
||||
fmt.Printf("整数:%d,平方:%d\n", val, val*val)
|
||||
case string:
|
||||
fmt.Printf("字符串:%q,长度:%d\n", val, len(val))
|
||||
default:
|
||||
fmt.Printf("未知类型:%T\n", val)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
processAny(10) // 整数:10,平方:100
|
||||
processAny("hello") // 字符串:"hello",长度:5
|
||||
processAny(3.14) // 未知类型:float64
|
||||
}
|
||||
Reference in New Issue
Block a user