当前位置:网站首页>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()
}
边栏推荐
- flutter 插件版本冲突的解决方法
- Test d'installation du cluster metaq
- 股票开户安全吗?上海股票开户。
- 海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》
- High performance and high availability computing architecture of "microblog comments" in microblog system
- 远程沟通高效的自我总结| 社区征文
- Software testing interface testing JMeter 5.5 installation tutorial
- High performance and high availability computing architecture of "Weibo comments"
- 声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
- The concept and significance of mean, variance, standard deviation and covariance
猜你喜欢

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

Remember the process of checking online MySQL deadlock. You should not only know curd, but also know the principle of locking

声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏

剑指 Offer 15. 二进制中1的个数

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

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

Incluxdb time series database system

二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树

Matplotlib histogram of Matplotlib visualization plt bar()

Redis client
随机推荐
AI赋能新零售,「智」胜之道在于生态思维|数智夜话直播精选摘录
剑指 Offer 14- I. 剪绳子
Some of my favorite websites
动态代理的实现原理
Solr基础操作5
搭建企业级NTP时间服务器
Which securities company should I choose to open an account online? Also, is it safe to open an account online?
雲和恩墨蓋國强,識別它、抓住它,在國產數據庫沸騰以前
声网自研传输层协议 AUT 的落地实践丨Dev for Dev 专栏
远程沟通高效的自我总结| 社区征文
按头安利 好看又实用的点胶机 SolidWorks模型素材看这里
Speech signal processing (III): speech signal analysis [continuous "analog signal" -- Sampling, quantization, coding -- > discrete "digital signal"]
软件测试 接口测试 Jmeter 5.5 安装教程
Why is JSX syntax so popular?
Label Troubleshooting: unable to open the marked image
Sword finger offer 14- ii Cutting rope II
开始“收割”!钉钉调整“钉钉Teambition”免费用人数上限,超十人将无法正常用
LC: maximum subarray and
Wechat applet: (update) cloud development wechat group contacts
Leetcode 1385. 两个数组间的距离值