Ai
4 Star 12 Fork 8

ShirDon-廖显东/零基础Go语言算法实战源码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
reflect1.go 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
ShirDon-廖显东 提交于 2024-04-22 14:56 +08:00 . first commit
// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
"reflect"
)
type User struct {
name string
userId int
score float64
}
type TypeString string
func print(x, y interface{}) {
t1 := reflect.TypeOf(x)
k1 := t1.Kind()
t2 := reflect.TypeOf(y)
k2 := t2.Kind()
fmt.Println("Type1:", t1)
fmt.Println("Kind1:", k1)
fmt.Println("Type2:", t2)
fmt.Println("Kind2:", k2)
fmt.Println("参数x中的值是:")
if reflect.ValueOf(x).Kind() == reflect.Struct {
value := reflect.ValueOf(x)
numberOfFields := value.NumField()
for i := 0; i < numberOfFields; i++ {
fmt.Printf("Type:%T || Value:%#v\n",
value.Field(i), value.Field(i))
fmt.Println("类型:", value.Field(i).Kind())
}
}
value := reflect.ValueOf(y)
fmt.Printf("y的值是 %#v", value)
}
func main() {
str := TypeString("666")
person := User{
name: "ShirDon",
userId: 6,
score: 99.5,
}
print(person, str)
}
//$ go run reflect1.go
//Type1: main.User
//Kind1: struct
//Type2: main.TypeString
//Kind2: string
//参数x中的值是:
//Type:reflect.Value || Value:"ShirDon"
//类型: string
//Type:reflect.Value || Value:6
//类型: int
//Type:reflect.Value || Value:99.5
//类型: float64
//y的值是 "666"
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shirdonl/goAlgorithms.git
git@gitee.com:shirdonl/goAlgorithms.git
shirdonl
goAlgorithms
零基础Go语言算法实战源码
3e77a12194dd

搜索帮助