当前位置:网站首页>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())
}
边栏推荐
- SEO学习的最好方式:搜索引擎
- L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
- The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
- Internal and external troubles of "boring ape" bayc
- Chrome view page FPS
- TS基础篇
- 顶测分享:想转行,这些问题一定要考虑清楚!
- 树莓派3B更新vim
- kubernetes集群搭建Zabbix监控平台
- MPLS experiment
猜你喜欢
WPF之MVVM
CDN acceleration and cracking anti-theft chain function
Path analysis model
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
How to find a medical software testing institution? First flight software evaluation is an expert
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
Interface automation test framework: pytest+allure+excel
18.多级页表与快表
Internal and external troubles of "boring ape" bayc
随机推荐
[advanced software testing step 1] basic knowledge of automated testing
Kubernetes cluster builds ZABBIX monitoring platform
变量的命名规则十二条
LeetCode Algorithm 2181. Merge nodes between zero
作者已死?AI正用藝術征服人類
19.段页结合的实际内存管理
Path analysis model
C - Inheritance - hidden method
这个高颜值的开源第三方网易云音乐播放器你值得拥有
数据仓库建设思维导图
Simple use of JWT
How to configure GUI guide development environment
Fast target recognition based on pytorch and fast RCNN
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
chrome查看页面fps
The best way to learn SEO: search engine
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
《从0到1:CTFer成长之路》书籍配套题目(周更)
leetcode6109. 知道秘密的人数(中等,周赛)
Librosa audio processing tutorial