当前位置:网站首页>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
边栏推荐
- IC popular science article: those things about Eco
- 常用的视图容器类组件
- 【数字IC验证快速入门】7、验证岗位中必备的数字电路基础知识(含常见面试题)
- . Net distributed transaction and landing solution
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
- B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
- 银河证券在网上开户安全吗?
- 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,
- Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
猜你喜欢

A way to calculate LNX

2.8、项目管理过程基础知识

Kubernetes resource object introduction and common commands (V) - (configmap & Secret)

Unity editor extended UI control

Applet page navigation

Leetcode (695) - the largest area of an island
![[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)](/img/90/aad9d7900d686efca10140717a5c5c.png)
[quick start of Digital IC Verification] 7. Basic knowledge of digital circuits necessary for verification positions (including common interview questions)

- Oui. Net Distributed Transaction and Landing Solution

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

Leetcode brush questions: binary tree 11 (balanced binary tree)
随机推荐
Unity editor extended UI control
Wechat applet regular expression extraction link
Ffplay document [easy to understand]
Applet page navigation
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
Frequent MySQL operations cause table locking problems
[quick start of Digital IC Verification] 2. Through an example of SOC project, understand the architecture of SOC and explore the design process of digital system
2.8、项目管理过程基础知识
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
[quick start of Digital IC Verification] 1. Talk about Digital IC Verification, understand the contents of the column, and clarify the learning objectives
B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05
Mongodb basic exercises
[quick start of Digital IC Verification] 3. Introduction to the whole process of Digital IC Design
Convolution free backbone network: Pyramid transformer to improve the accuracy of target detection / segmentation and other tasks (with source code)
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
y57.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(三十)
Bzoj 3747 poi2015 kinoman segment tree
Informatics Olympiad 1337: [example 3-2] word search tree | Luogu p5755 [noi2000] word search tree
2.8 basic knowledge of project management process
【数字IC验证快速入门】9、Verilog RTL设计必会的有限状态机(FSM)