当前位置:网站首页>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())
}
边栏推荐
- C - Inheritance - polymorphism - virtual function member (lower)
- 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
- LeetCode Algorithm 2181. Merge nodes between zero
- 1091: two or three things in childhood (multi instance test)
- Kubernetes cluster builds ZABBIX monitoring platform
- leetcode1020. Number of enclaves (medium)
- 网络安全基础介绍
- Cif10 actual combat (resnet18)
- Fast target recognition based on pytorch and fast RCNN
- 攻防世界 MISC中reverseMe简述
猜你喜欢
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
Cif10 actual combat (resnet18)
1189. Maximum number of "balloons"
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
“无聊猿” BAYC 的内忧与外患
WPF之MVVM
升级版手机检测微信工具小程序源码-支持多种流量主模式
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
Depth residual network
Solution to the problem of breakthrough in OWASP juice shop shooting range
随机推荐
前缀和数组系列
Blue Bridge Cup zero Foundation National Championship - day 20
Visitor tweets about how you can layout the metauniverse
接口自动化测试框架:Pytest+Allure+Excel
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
Lesson 12 study notes 2022.02.11
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
Short video, more and more boring?
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
Introduction to ros2 installation and basic knowledge
(4) Web security | penetration testing | network security web site source code and related analysis
mysql如何合并数据
BUU的MISC(不定时更新)
leetcode841. 钥匙和房间(中等)
网络安全基础介绍
GET 和 POST 请求类型的区别
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
SSM学习
[daily question] 729 My schedule I
TS基础篇