当前位置:网站首页>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
边栏推荐
- Leetcode(347)——前 K 个高频元素
- 欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
- CCPC 2021 Weihai - G. shinyruo and KFC (combination number, tips)
- y57.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(三十)
- nprogress插件 进度条
- Practical demonstration: how can the production research team efficiently build the requirements workflow?
- USACO3.4 “破锣摇滚”乐队 Raucous Rockers - DP
- Composition of applet code
- Ffplay document [easy to understand]
- Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)
猜你喜欢

A way to calculate LNX

14、Transformer--VIT TNT BETR

【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)
![[record of question brushing] 1 Sum of two numbers](/img/ea/4e981edd5570b49b4fa909ac8da6c4.png)
[record of question brushing] 1 Sum of two numbers

Guidelines for application of Shenzhen green and low carbon industry support plan in 2023

Fundamentals - configuration file analysis

Applet page navigation

.Net分布式事務及落地解决方案

Station B up builds the world's first pure red stone neural network, pornographic detection based on deep learning action recognition, Chen Tianqi's course progress of machine science compilation MLC,

Classic implementation of the basic method of intelligent home of Internet of things
随机推荐
零道云新UI设计中
Frequent MySQL operations cause table locking problems
go 文件路径操作
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Kubernetes resource object introduction and common commands (V) - (configmap & Secret)
Applet page navigation
- Oui. Net Distributed Transaction and Landing Solution
2022北京眼睛健康用品展,护眼产品展,中国眼博会11月举办
2.8 basic knowledge of project management process
Leetcode skimming: binary tree 12 (all paths of binary tree)
【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
鸿蒙os第四次学习
Nprogress plug-in progress bar
港股将迎“最牛十元店“,名创优品能借IPO突围?
Classic implementation method of Hongmeng system controlling LED
IC科普文:ECO的那些事儿
Scala基础【HelloWorld代码解析,变量和标识符】
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗