当前位置:网站首页>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())
}
边栏推荐
- 19. Actual memory management of segment page combination
- Uni app practical project
- 中青看点阅读新闻
- MPLS experiment
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- Internal and external troubles of "boring ape" bayc
- 指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
- 网络安全基础介绍
- leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
- You deserve this high-value open-source third-party Netease cloud music player
猜你喜欢
You deserve this high-value open-source third-party Netease cloud music player
Top test sharing: if you want to change careers, you must consider these issues clearly!
Misc of BUU (update from time to time)
(4) Web security | penetration testing | network security web site source code and related analysis
Short video, more and more boring?
18. Multi level page table and fast table
【每日一题】729. 我的日程安排表 I
Thought map of data warehouse construction
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
随机推荐
Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
Multithreading and concurrent programming (2)
配置树莓派接入网络
Solution to the problem of breakthrough in OWASP juice shop shooting range
网络安全基础介绍
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
顶测分享:想转行,这些问题一定要考虑清楚!
Lesson 12 study notes 2022.02.11
UWA pipeline version 2.2.1 update instructions
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)
BIO模型实现多人聊天
Refer to how customer push e-commerce does content operation
CDN acceleration and cracking anti-theft chain function
leetcode841. Keys and rooms (medium)
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Development of entity developer database application
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
leetcode1020. 飞地的数量(中等)
多线程和并发编程(二)
【JDBC】快速入门教程