当前位置:网站首页>Go learning --- use reflection to judge whether the value is valid
Go learning --- use reflection to judge whether the value is valid
2022-07-06 07:11:00 【Duck boss】
One 、 Use reflection to determine whether the value is valid
package main
import (
"fmt"
"reflect"
)
// Define a structure
type Tom struct {
}
func main() {
var a *int
isNilA := reflect.ValueOf(a).IsNil()
fmt.Println(" Determine whether the pointer is initialized :",isNilA)
var b *int = new(int)
isNilB := reflect.ValueOf(b).IsNil()
fmt.Println(" Determine whether the pointer is initialized :",isNilB)
var c int
zero := reflect.ValueOf(c).IsZero()
fmt.Println("c Whether the value of is zero :",zero)
c = 1
zeros := reflect.ValueOf(c).IsZero()
fmt.Println("c Whether the value of is zero :",zeros)
var d int
// To see if it works
valid := reflect.ValueOf(d).IsValid()
fmt.Println("d Whether it works :",valid)
d = 2
fmt.Println("d Whether it works :",reflect.ValueOf(d).IsValid())
tom := Tom{}
fmt.Println(" Whether the internal attribute name of the structure is valid ",reflect.ValueOf(tom).FieldByName("name").IsValid())
fmt.Println(" Whether the structure method is effective :",reflect.ValueOf(tom).MethodByName("name").IsValid())
m := make(map[interface{}]interface{})
fmt.Println(reflect.ValueOf(m).MapIndex(reflect.ValueOf(3)).IsValid())
}
边栏推荐
猜你喜欢
数据仓库建设思维导图
Entity Developer数据库应用程序的开发
What is the biggest problem that fresh e-commerce is difficult to do now
Huawei equipment configuration ospf-bgp linkage
1189. Maximum number of "balloons"
Raspberry pie serial port login and SSH login methods
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
How are the open source Netease cloud music API projects implemented?
前缀和数组系列
19. Actual memory management of segment page combination
随机推荐
Leetcode 78: subset
这个高颜值的开源第三方网易云音乐播放器你值得拥有
攻防世界 MISC中reverseMe简述
Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
#systemverilog# 可綜合模型的結構總結
《从0到1:CTFer成长之路》书籍配套题目(周更)
Development of entity developer database application
19.段页结合的实际内存管理
Cookie技术&Session技术&ServletContext对象
BUU的MISC(不定时更新)
1189. Maximum number of "balloons"
ROS学习_基础
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
SEO学习的最好方式:搜索引擎
C - Inheritance - hidden method
Entity Developer数据库应用程序的开发
Short video, more and more boring?
[some special grammars about C]
WPF之MVVM
SSM learning