当前位置:网站首页>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())
}
边栏推荐
- Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
- Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
- “无聊猿” BAYC 的内忧与外患
- leetcode704. Binary search (find an element, simple, different writing)
- 微信公众号无限回调授权系统源码 全网首发
- 从autojs到冰狐智能辅助的心里历程
- Multithreading and concurrent programming (2)
- What does UDP attack mean? UDP attack prevention measures
- Arduino tutorial - Simon games
- Setting and using richview trvstyle template style
猜你喜欢

What is the biggest problem that fresh e-commerce is difficult to do now

Prefix and array series

leetcode35. 搜索插入位置(简单,找插入位置,不同写法)

因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫

Misc of BUU (update from time to time)

Raspberry pie serial port login and SSH login methods

SEO学习的最好方式:搜索引擎

Proteus -- Serial Communication parity flag mode

1189. Maximum number of "balloons"

(4) Web security | penetration testing | network security web site source code and related analysis
随机推荐
Multithreading and concurrent programming (2)
Raspberry pie 3B update VIM
How to find a medical software testing institution? First flight software evaluation is an expert
Yield method of tread
Establishment and operation of cloud platform open source project environment
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
Setting and using richview trvstyle template style
Top test sharing: if you want to change careers, you must consider these issues clearly!
Cookie技术&Session技术&ServletContext对象
升级版手机检测微信工具小程序源码-支持多种流量主模式
RichView TRVStyle 模板样式的设置与使用
顶测分享:想转行,这些问题一定要考虑清楚!
多线程和并发编程(二)
Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
Pymongo gets a list of data
Structure summary of SystemVerilog integrable model
从autojs到冰狐智能辅助的心里历程
1091: two or three things in childhood (multi instance test)
SSM学习
Embed UE4 program into QT interface display