当前位置:网站首页>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
边栏推荐
- 1. Strengthen learning basic knowledge points
- 如何形成规范的接口文档
- Some problems encountered in cocos2d-x project summary
- Leetcode skimming: binary tree 12 (all paths of binary tree)
- Mysql频繁操作出现锁表问题
- 炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
- Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
- 信息学奥赛一本通 1340:【例3-5】扩展二叉树
- [quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
- Notes on key vocabulary in the English original of the biography of jobs (12) [chapter ten & eleven]
猜你喜欢
14、Transformer--VIT TNT BETR
CTF reverse Foundation
【数字IC验证快速入门】1、浅谈数字IC验证,了解专栏内容,明确学习目标
IC科普文:ECO的那些事儿
1、强化学习基础知识点
港股将迎“最牛十元店“,名创优品能借IPO突围?
Guidelines for application of Shenzhen green and low carbon industry support plan in 2023
Applet event binding
Frequent MySQL operations cause table locking problems
【数字IC验证快速入门】6、Questasim 快速上手使用(以全加器设计与验证为例)
随机推荐
走入并行的世界
Scala basics [HelloWorld code parsing, variables and identifiers]
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
Zero cloud new UI design
Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)
When JS method passes long type ID value, precision loss will occur
Applet event binding
Unity editor extended UI control
Codeforces Round #804 (Div. 2) - A, B, C
Scala基础【HelloWorld代码解析,变量和标识符】
14、Transformer--VIT TNT BETR
DP: tree DP
A solution to PHP's inability to convert strings into JSON
Mongodb/ document operation
全国爱眼教育大会,2022第四届北京国际青少年眼健康产业展会
强化学习-学习笔记4 | Actor-Critic
2.8、项目管理过程基础知识
死信队列入门(两个消费者,一个生产者)
Applet global configuration
Relationship between mongodb documents