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

推荐订阅源

美团技术团队
W
WeLiveSecurity
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
F
Full Disclosure
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
G
Google Developers Blog
C
Check Point Blog
GbyAI
GbyAI
A
About on SuperTechFans
V
Vulnerabilities – Threatpost
T
The Blog of Author Tim Ferriss
T
Tor Project blog
AWS News Blog
AWS News Blog
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
MongoDB | Blog
MongoDB | Blog
Latest news
Latest news
aimingoo的专栏
aimingoo的专栏
U
Unit 42
Y
Y Combinator Blog
P
Privacy International News Feed
Cisco Talos Blog
Cisco Talos Blog
S
Securelist
S
Schneier on Security
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
C
Cisco Blogs
Webroot Blog
Webroot Blog
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
P
Privacy & Cybersecurity Law Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
罗磊的独立博客
Cloudbric
Cloudbric
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Hacker News: Ask HN
Hacker News: Ask HN
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Security Blog
Microsoft Security Blog

博客园 - 柒零壹

Ubuntu 24.04 安装最新版podman@5.6.1 [转贴]在前端如何玩转 Word 文档 一个命令行参数解决open-webui镜像启动失败的问题(huggingface网站访问失败问题) Go Template 常用疑难知识点 从数据库中随机选取数据(基于golang,xorm) go-ElasticSearch TypedClient学习笔记 golang用pgx查询数据时如何将查询结果方便的放入Map中 go语言如何使用elastic官方客户端go-elasticsearch/v8实现数据批量更新 【转】网络常用颜文字(文字表情) GoCV下实现多图片单窗口内同时显示 解决GoCV/OpenCV不支持中文的问题 golang中xorm自动维护表结构自动导入数据的实现 [golang] gin的中间中调用Abort方法导致的带附件的表单提交时,浏览器报net::ERR_CONNECTION_RESET错误的原因及解决方法 浏览器中javascript简易实现json数据保存到客户端 最简单搭建前端轻量级项目开发服务 [转]Three.js做一个酷炫的城市展示可视化大屏 [转]css实现不同样式的tooltip对话框小三角 [原]升级项目到Rails7.0.3,使用自己手动方案编译打包css及js资源 puma web server如何监听所有IP地址
[golang]filepath.Glob的缺陷,不支持多级目录
柒零壹 · 2023-02-02 · via 博客园 - 柒零壹

  最近在使用Gin框架的模板加载过程中,发现其对于多级子目录中的模板支持有问题(仅仅支持一级子目录),后经过查看其源码发现是filepath包的Glob方法的问题。下面先说结论:

  1. 多级目录支持有问题
  2. 不支持shell下的Glob的扩展特性

  下面是我的模板目录结构:

views
│  401.html
│  home.tmpl
│
├─admin
│  ├─authorize
│  │      index.tmpl
│  │
│  └─dicts
│          index.tmpl
│
├─signin
│      signin.html
│      signout.html
│
└─users
        index.html

View Code

  按照其他语言中Glob的使用经验,应该可以轻松的列出views目录中的.tmpl文件,于是我尝试下面的模式,结果竟然为空

filepath.Glob("./views/**/*.tmpl")

  下面是测试各种模式组合的代码:

 func testGlob(){  
  patternList := []string{
        "./views/*",
        "./views/**", /** 和一个星号一样,列出直接子目录和文件 */
        "./views/*.tmpl",
        "./views/**/*.tmpl",
        "./views/**/*",
        "./views/**/*/*.tmpl",
        "./views/*/*/*.tmpl",
        "./views/**/**/*.tmpl",
        "./views/{*.tmpl,**/*.tmpl,**/**/*.tmpl}",
    }
    for _, pattern = range patternList {
        files, err := filepath.Glob(pattern)
        if err != nil {
            slog.Error(fmt.Sprintf("filepath.Glob(\"%s\") error", pattern), err)
        }
        fmt.Printf("filepath.Glob(\"%s\") result files=[%s]\n", pattern, strings.Join(files, ";"))
    }
}

  运行结果如下:

filepath.Glob("./views/*") result files=[views\401.html;views\admin;views\home.tmpl;views\signin;views\users]
filepath.Glob("./views/**") result files=[views\401.html;views\admin;views\home.tmpl;views\signin;views\users]
filepath.Glob("./views/*.tmpl") result files=[views\home.tmpl]
filepath.Glob("./views/**/*.tmpl") result files=[]
filepath.Glob("./views/**/*") result files=[views\admin\authorize;views\admin\dicts;views\signin\signin.html;views\signin\signout.html;views\users\index.html]

filepath.Glob("./views/**/*/*.tmpl") result files=[views\admin\authorize\index.tmpl;views\admin\dicts\index.tmpl]
filepath.Glob("./views/*/*/*.tmpl") result files=[views\admin\authorize\index.tmpl;views\admin\dicts\index.tmpl]
filepath.Glob("./views/**/**/*.tmpl") result files=[views\admin\authorize\index.tmpl;views\admin\dicts\index.tmpl]
filepath.Glob("./views/{*.tmpl,**/*.tmpl,**/**/*.tmpl}") result files=[]