当前位置:网站首页>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())
}
边栏推荐
- Kubernetes cluster builds ZABBIX monitoring platform
- Thought map of data warehouse construction
- 树莓派串口登录与SSH登录方法
- 升级版手机检测微信工具小程序源码-支持多种流量主模式
- 19.段页结合的实际内存管理
- Establishment and operation of cloud platform open source project environment
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
- SEO学习的最好方式:搜索引擎
- 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
猜你喜欢

Blue Bridge Cup zero Foundation National Championship - day 20

Path analysis model

kubernetes集群搭建Zabbix监控平台

C - Inheritance - polymorphism - virtual function member (lower)

Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案

leetcode841. 钥匙和房间(中等)

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

A brief introduction of reverseme in misc in the world of attack and defense

Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)

mysql如何合并数据
随机推荐
How to configure GUI guide development environment
chrome查看页面fps
GET 和 POST 请求类型的区别
Bio model realizes multi person chat
ROS学习_基础
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
(4) Web security | penetration testing | network security web site source code and related analysis
The differences and advantages and disadvantages between cookies, seeion and token
《从0到1:CTFer成长之路》书籍配套题目(周更)
作者已死?AI正用艺术征服人类
BIO模型实现多人聊天
UDP攻击是什么意思?UDP攻击防范措施
leetcode841. 钥匙和房间(中等)
C language_ Double create, pre insert, post insert, traverse, delete
变量的命名规则十二条
呆错图床系统源码图片CDN加速与破解防盗链功能
Entity Developer数据库应用程序的开发
[daily question] 729 My schedule I
Establishment and operation of cloud platform open source project environment
Uni app practical project