当前位置:网站首页>Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
2022-06-29 23:43:00 【Deng_ Xian_ Sheng】
//SafeValue
// Can pass pointer , You can also pass values
// Pass the pointer and return the pointer , Return value if value is passed
// A null pointer will return a pointer with a zero value of this type
func SafeValue[T any](v T) T {
vt := reflect.TypeOf(v)
switch vt.Kind() {
case reflect.Ptr:
vv := reflect.ValueOf(v)
if vv.IsNil() {
return reflect.New(vt.Elem()).Interface().(T)
}
return v
default:
return v
}
}
// SafeStruct
// Pointer must be passed in
// It will set the field of null pointer to the pointer of zero value of this type
// Passing in a null pointer returns a structure with zero values for all fields
func SafeStruct(v interface{
}) interface{
} {
vt := reflect.TypeOf(v)
vv := reflect.ValueOf(v)
if vv.IsNil() {
vv = reflect.ValueOf(reflect.New(vt.Elem()).Interface()).Elem()
} else {
vv = vv.Elem()
}
for i := 0; i < vv.NumField(); i++ {
if vv.Field(i).Kind() == reflect.Ptr && vv.Field(i).IsNil() {
vv.Field(i).Set(reflect.New(vv.Field(i).Type().Elem()))
}
}
return vv.Interface()
}
边栏推荐
- 简单理解B树和B+树
- Label Troubleshooting: unable to open the marked image
- Cacti最大监控数测试
- 剑指 Offer 14- II. 剪绳子 II
- Pain points and solutions of M1 notebook home office | community essay solicitation
- Solr基础操作4
- 动态代理的实现原理
- Wechat applet: (update) cloud development wechat group contacts
- Principe de réalisation de l'agent dynamique
- High performance and high availability computing architecture of "Weibo comments"
猜你喜欢

C指针进阶2-->函数指针数组 回调函数简化计算器代码,基于回调函数模拟实现qsort函数

Wechat applet: (update) cloud development wechat group contacts

记一次排查线上MySQL死锁过程,不能只会curd,还要知道加锁原理

Software testing interface testing JMeter 5.5 installation tutorial

Yunhe enmo, gaiguoqiang, identify it and grasp it before the domestic database boils

剑指 Offer 13. 机器人的运动范围

Constexpr function

Pain points and solutions of M1 notebook home office | community essay solicitation

Node data collection and remote flooding transmission of label information

The concept and significance of mean, variance, standard deviation and covariance
随机推荐
How to solve the problem that the computer time is not automatically updated after proofreading
Matlab exercises -- program control process exercise
Solr基础操作2
C指针进阶1-->字符指针,数组指针,指针与数组传参,函数指针
C pointer advanced 2-- > function pointer array callback function simplifies calculator code, and implements qsort function based on callback function simulation
High performance and high availability computing architecture of "microblog comments" in microblog system
[LeetCode] 只出现一次的数字【136】
Label Troubleshooting: unable to open the marked image
333333333333333333333333333333
除子
Solr basic operation 5
“微博评论”的高性能高可用计算架构
xutils3传集合
FPGA开发(2)——IIC通信
Which securities company should I choose to open an account online? Also, is it safe to open an account online?
C指针进阶2-->函数指针数组 回调函数简化计算器代码,基于回调函数模拟实现qsort函数
我想知道今天还可以开户么?另外想问,现在在线开户安全么?
剑指 Offer 14- I. 剪绳子
二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树
Use of jetpack's room in combination with flow