惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

崔八由

内网设备被挖矿了 对华宸未来债券基金更名的总结 凸(艹皿艹 ) 推特被封了 第一次装机过程与记录 记录一下今年治牙的过程 预测未来 最终还是投向了obsidian pve lxc容器使用vnpy的问题 扔垃圾的感受 解决macos sierra重新安装时报错-准备安装时发生错误 记录一下使用vercel搭建umami中需要注意的地方 还是不能太依赖自建服务 Android应用在上架Google Play时封闭式测试的一些建议 JVM中的Shallow Size 和 Retained Size解释 acme.sh自动生成泛域名证书的几个坑 老了 go学习笔记之指针 pve主机使用smart_ctl监控磁盘信息 tailscale的设置与安装
go中获取某个包下定义的所有变量
崔八由 · 2024-01-12 · via 崔八由

发表于|更新于|编程go

|浏览量:|

有这样一个场景, 我们在e这个包下, 有一个err_code.go文件,在其中自定义了一个错误码结构体和一些错误码,内容如下:

1
2
3
4
5
6
7
8
9
10
11
package e

type ErrCode struct {
Code int
Msg string
}
var (
Success = ErrCode{Code: 10000, Msg: ""}
UnknownErr = ErrCode{Code: 10001, Msg: "未知错误"}
InvalidParams = ErrCode{Code: 10002, Msg: "参数不合法"}
)

现在我们想获取到所有这些预先定义的错误码的code,应该如何做呢?

可以借助go标准库中AST语法树的能力遍历所有变量,然后过滤得到我们想要的变量类型, 具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
func Test_IntlDictMatchErrCodeEnum(t *testing.T) {
fset := token.NewFileSet()

pkgs, err := parser.ParseDir(fset, "../pkg/e", nil, parser.ParseComments)
if err != nil {
log.Fatal(err)
}

pkg := pkgs["e"]


for _, file := range pkg.Files {

for _, decl := range file.Decls {

if genDecl, ok := decl.(*ast.GenDecl); ok && genDecl.Tok == token.VAR {

for _, spec := range genDecl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
codeElt := valueSpec.Values[0]
if y, ok := codeElt.(*ast.CompositeLit); ok {
if j, ok := y.Elts[0].(*ast.KeyValueExpr); ok {
if l, ok := j.Value.(*ast.BasicLit); ok {

fmt.Printf("code:%s\n", l.Value)
}
}
}
}
}
}
}
}
}

avatar

七只鸟离开了夏天,飞向了秋天