当前位置:网站首页>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
边栏推荐
- Ffplay document [easy to understand]
- CVPR 2022 | common 3D damage and data enhancement
- CVPR 2022 | 常见3D损坏和数据增强
- Scala basics [HelloWorld code parsing, variables and identifiers]
- Rainbond 5.7.1 支持对接多家公有云和集群异常报警
- C langue OJ obtenir PE, ACM démarrer OJ
- .Net分布式事务及落地解决方案
- Is it safe for CICC fortune to open an account online?
- mongodb文档间关系
- [Yugong series] go teaching course in July 2022 004 go code Notes
猜你喜欢

PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug

National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition

港股将迎“最牛十元店“,名创优品能借IPO突围?

JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)

鸿蒙系统控制LED的实现方法之经典

Rainbond 5.7.1 支持对接多家公有云和集群异常报警

解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接

Leetcode skimming: binary tree 12 (all paths of binary tree)
![[record of question brushing] 1 Sum of two numbers](/img/ea/4e981edd5570b49b4fa909ac8da6c4.png)
[record of question brushing] 1 Sum of two numbers

1、强化学习基础知识点
随机推荐
mongodb/文档操作
Fundamentals - configuration file analysis
July 4, 2022 - July 10, 2022 (UE4 video tutorial MySQL)
Mysql频繁操作出现锁表问题
IC科普文:ECO的那些事儿
Leetcode brush questions: binary tree 11 (balanced binary tree)
ROS2专题【01】:win10上安装ROS2
ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Introduction to dead letter queue (two consumers, one producer)
Leetcode (347) - top k high frequency elements
How to retrieve the root password of MySQL if you forget it
Leetcode brush question: binary tree 13 (the same tree)
Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)
sort和投影
关于BRAM IP复位的优先级
C langue OJ obtenir PE, ACM démarrer OJ
USACO3.4 “破锣摇滚”乐队 Raucous Rockers - DP
[C language] three implementations of quick sorting and optimization details
信息学奥赛一本通 1340:【例3-5】扩展二叉树