当前位置:网站首页>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
边栏推荐
- Mongodb/ document operation
- Introduction to dead letter queue (two consumers, one producer)
- Document method
- ROS2专题【01】:win10上安装ROS2
- Classic implementation method of Hongmeng system controlling LED
- 【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)
- IC科普文:ECO的那些事儿
- Leetcode brush question: binary tree 14 (sum of left leaves)
- 【数字IC验证快速入门】6、Questasim 快速上手使用(以全加器设计与验证为例)
- Simple understanding of interpolation search
猜你喜欢
A way to calculate LNX
Classic implementation method of Hongmeng system controlling LED
如何形成规范的接口文档
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
[quick start of Digital IC Verification] 9. Finite state machine (FSM) necessary for Verilog RTL design
14、Transformer--VIT TNT BETR
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)
Zero cloud new UI design
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
随机推荐
Leetcode(347)——前 K 个高频元素
Mysql频繁操作出现锁表问题
Fundamentals - configuration file analysis
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
Guidelines for application of Shenzhen green and low carbon industry support plan in 2023
Minimum commission for stock trading account opening, where to open an account with low commission? Is it safe to open an account on your mobile phone
IC popular science article: those things about Eco
Scala基础【HelloWorld代码解析,变量和标识符】
js方法传Long类型id值时会出现精确损失
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
CVPR 2022 | 常见3D损坏和数据增强
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
ROS2专题【01】:win10上安装ROS2
July 4, 2022 - July 10, 2022 (UE4 video tutorial MySQL)
About the priority of Bram IP reset
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
Applet page navigation
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)