当前位置:网站首页>Go file path operation
Go file path operation
2022-07-05 20:34:00 【lishuangquan1987】
The files in the test directory are as follows :
- main.go
- 1.txt
- ./a/b/2.txt
Get all files in the specified folder ( Not a recursive )
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())
}
}
Output :
PS E:\Go Project\pathtest> go run main.go
1.txt
main.go
Get all files in the specified folder ( recursive )
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
}
Output :
PS E:\Go Project\pathtest> go run main.go
1.txt
a\b\2.txt
main.go
Path splicing
route :./a/b/2.txt
Realize splicing :
func main() {
baseDir := "./"
aDir := "a"
bDir := "b"
file := "2.txt"
absFilePath := filepath.Join(baseDir, aDir, bDir, file)
fmt.Println(absFilePath)
}
Output :
PS E:\Go Project\pathtest> go run main.go
a\b\2.txt
边栏推荐
- y57.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(三十)
- Leetcode: binary tree 15 (find the value in the lower left corner of the tree)
- Informatics Orsay all in one 1339: [example 3-4] find the post order traversal | Valley p1827 [usaco3.4] American Heritage
- Introduction to dead letter queue (two consumers, one producer)
- Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
- Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
- 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
- 【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)
- 【数字IC验证快速入门】3、数字IC设计全流程介绍
- 信息学奥赛一本通 1337:【例3-2】单词查找树 | 洛谷 P5755 [NOI2000] 单词查找树
猜你喜欢
随机推荐
Schema and model
Applet event binding
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)
死信队列入门(两个消费者,一个生产者)
Scala basics [HelloWorld code parsing, variables and identifiers]
CCPC 2021威海 - G. Shinyruo and KFC(组合数,小技巧)
无卷积骨干网络:金字塔Transformer,提升目标检测/分割等任务精度(附源代码)...
Guidelines for application of Shenzhen green and low carbon industry support plan in 2023
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
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
mongodb文档间关系
Leetcode (695) - the largest area of an island
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design
全国爱眼教育大会,2022第四届北京国际青少年眼健康产业展会
19 mongoose modularization
Applet page navigation
银河证券在网上开户安全吗?