当前位置:网站首页>go 文件路径操作
go 文件路径操作
2022-07-05 20:28:00 【lishuangquan1987】
测试目录下的文件如下:
- main.go
- 1.txt
- ./a/b/2.txt
获取指定文件夹下的所有文件(不递归)
package main
import (
"fmt"
"io/ioutil"
)
func main() {
baseDir := "./"
fileInfos, _ := ioutil.ReadDir(baseDir)
for _, f := range fileInfos {
if f.IsDir(){
continue
}
fmt.Println(f.Name())
}
}
输出:
PS E:\Go Project\pathtest> go run main.go
1.txt
main.go
获取指定文件夹下的所有文件(递归)
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
)
func main() {
baseDir := "./"
files := ReadFiles(baseDir)
for _, file := range files {
fmt.Println(file)
}
}
func ReadFiles(dir string) []string {
result := make([]string, 0)
fileInfos, _ := ioutil.ReadDir(dir)
for _, f := range fileInfos {
if f.IsDir() {
result = append(result, ReadFiles(filepath.Join(dir, f.Name()))...)
} else {
result = append(result, filepath.Join(dir, f.Name()))
}
}
return result
}
输出:
PS E:\Go Project\pathtest> go run main.go
1.txt
a\b\2.txt
main.go
路径拼接
路径:./a/b/2.txt
实现拼接:
func main() {
baseDir := "./"
aDir := "a"
bDir := "b"
file := "2.txt"
absFilePath := filepath.Join(baseDir, aDir, bDir, file)
fmt.Println(absFilePath)
}
输出:
PS E:\Go Project\pathtest> go run main.go
a\b\2.txt
边栏推荐
猜你喜欢
Wechat applet regular expression extraction link
CTF reverse Foundation
Frequent MySQL operations cause table locking problems
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
CTF逆向基础
[Yugong series] go teaching course in July 2022 004 go code Notes
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)
Unity编辑器扩展 UI控件篇
Oracle tablespace management
随机推荐
点云文件的.dat文件读取保存
Fundamentals - configuration file analysis
2022年7月4日-2022年7月10日(ue4视频教程mysql)
How to form standard interface documents
Scala基础【HelloWorld代码解析,变量和标识符】
Ros2 topic [01]: installing ros2 on win10
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
DP: tree DP
Common view container class components
Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
- Oui. Net Distributed Transaction and Landing Solution
Codeforces Round #804 (Div. 2) - A, B, C
鸿蒙os第四次学习
19 mongoose modularization
CTF reverse Foundation
C langue OJ obtenir PE, ACM démarrer OJ
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
信息学奥赛一本通 1339:【例3-4】求后序遍历 | 洛谷 P1827 [USACO3.4] 美国血统 American Heritage
Bzoj 3747 poi2015 kinoman segment tree
Is it safe for CICC fortune to open an account online?