当前位置:网站首页>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())
}
边栏推荐
- MPLS experiment
- LeetCode Algorithm 2181. Merge nodes between zero
- leetcode841. Keys and rooms (medium)
- Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
- What does UDP attack mean? UDP attack prevention measures
- On the world of NDK (2)
- 19. Actual memory management of segment page combination
- Librosa audio processing tutorial
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
猜你喜欢
随机推荐
Is software testing outsourcing going or not? Three years' real outsourcing experience tells you
Blue Bridge Cup zero Foundation National Championship - day 20
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
Markdown 中设置图片图注
Short video, more and more boring?
作者已死?AI正用藝術征服人類
LeetCode 78:子集
[some special grammars about C]
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
WPF之MVVM
Visitor tweets about how you can layout the metauniverse
Arduino tutorial - Simon games
升级版手机检测微信工具小程序源码-支持多种流量主模式
Compile, connect -- notes-2
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
微信公众号无限回调授权系统源码 全网首发
【Hot100】739. 每日溫度
PCL实现选框裁剪点云
Three methods of adding color to latex text
【Hot100】739. 每日温度