惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy International News Feed
I
Intezer
T
Tenable Blog
S
Schneier on Security
Project Zero
Project Zero
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
Know Your Adversary
Know Your Adversary
博客园 - 司徒正美
The Cloudflare Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
博客园 - 叶小钗
宝玉的分享
宝玉的分享
L
LINUX DO - 热门话题
aimingoo的专栏
aimingoo的专栏
S
Secure Thoughts
Forbes - Security
Forbes - Security
T
The Exploit Database - CXSecurity.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
罗磊的独立博客
IT之家
IT之家
H
Hacker News: Front Page
I
InfoQ
云风的 BLOG
云风的 BLOG
S
Security Affairs
M
MIT News - Artificial intelligence
GbyAI
GbyAI
Jina AI
Jina AI
Help Net Security
Help Net Security
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
Attack and Defense Labs
Attack and Defense Labs
The Register - Security
The Register - Security
V
V2EX
G
Google Developers Blog
D
DataBreaches.Net
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
J
Java Code Geeks
W
WeLiveSecurity
Cloudbric
Cloudbric
T
Tor Project blog

博客园 - dribs

PD分离 基于GPT2训练构建医疗问诊机器人 物流行业信息咨询智能问答系统 day8 golang-chan-协程-定时器-锁-等待组 day7 golang GMP golang面试题单向链表和双向链表 golang 自行实现一个base64加密 golang标准库log+第三方zerolog golang标准库序列化反序列化 json + 第三方msgpack golang-day1 进制 golang-标准库文件操作 golang-标准库时间time day6 golang-标准库(随时更新) golang--day5结构体+结构体排序+错误处理+结构体和接口实现面向对象的例子 golang-day4函数 ceph warn oldest client/flush tid ceph 扩容后healt-error k8s系列---pod手动驱逐 k8s系列---Chart(下)
golang 标准库 目录操作
dribs · 2023-03-02 · via 博客园 - dribs

2023-03-02 21:58  dribs  阅读(84)  评论()    收藏  举报

package main

import (
	"fmt"
	"io/fs"
	"io/ioutil"
	"os"
	"path/filepath"
)

//路径拼接
func example1() {
	p1 := "a/b" + "/" + "c/d" + "/" + "f"
	p2 := filepath.Join("a/b", "c/d", "f")
	dir := filepath.Dir(p1)  //  a\b\c\d
	dir2 := filepath.Dir(p2) //  a\b\c\d
	fmt.Println(dir)
	fmt.Println(dir2)
}

//路径分解
func example2() {
	p1 := "a/b/c/d/f/main.ini"
	dir, file := filepath.Split(p1) //  a/b/c/d/f/ main.ini
	fmt.Println(dir, file)
	fmt.Println(filepath.Dir(p1))  //  a\b\c\d\f
	fmt.Println(filepath.Base(p1)) //  main.ini
	fmt.Println(filepath.Ext(p1))  //.ini

	fmt.Println(os.Getwd())       //当前路径
	fmt.Println(os.UserHomeDir()) //家目录

}

//文件存在性
func example3() {
	p1 := "e:/test.txt" //真实存在
	_, err := os.Stat(p1)
	fmt.Println(err)
	//用os.IsExist(err)容易让人疑惑,建议使用os.IsNotExist(err)。
	//true false false false
	fmt.Println(err == nil, os.IsExist(err), err != nil, os.IsNotExist(err))
	p1 = "a/b/c/d/e"
	_, err = os.Stat(p1)
	fmt.Println(err)
	//用os.IsExist(err)容易让人疑惑,建议使用os.IsNotExist(err)。
	//false false true tru
	fmt.Println(err == nil, os.IsExist(err), err != nil, os.IsNotExist(err))
}

//stat和绝对路
func example4() {
	p1 := "/a/b/c/d/f/main.ini"
	dir := filepath.Dir(p1)
	err := os.MkdirAll(dir, os.ModePerm) //创建所有父目录
	if err != nil {
		fmt.Println(err)
		return
	}
	f, err := os.Create(p1)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer f.Close()
	//文件stat
	info, err := os.Stat(p1)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(
		info.Name(),
		info.IsDir(),
		info.Mode(),
		info.ModTime(),
		info.Size(),
	)

	//绝对路径
	fmt.Println("=========绝对路径===========")
	fmt.Println(filepath.Abs(p1))                 //E:\a\b\c\d\f\main.ini
	fmt.Println(filepath.IsAbs(p1))               //是否是绝对路径 false
	fmt.Println(filepath.Abs("a/b"))              //取绝对路径
	fmt.Println(os.Getwd())                       //当前工作路径
	fmt.Println(filepath.Rel("/a/b", "/a/b/c/d")) //计算相对路径 c/d

}

//遍历
//filepath.WalkDir 和 filepath.Walk
//遍历递归目录树 每遍历到一个节点,都会执行回调函数,只不过返回的参数略有不同
//都不跟踪软连接 内部都是按照字典序输出 深度优先
//ioutil.ReadDir(path) 不递归遍历当前目录
func example5() {
	p1 := "/a"
	//walkdir和walk遍历递归遍历 包含自身
	filepath.WalkDir(p1, func(path string, d fs.DirEntry, err error) error {
		fmt.Println(path, d.IsDir(), d.Name(), err) //递归读出目录和文件
		return err
	})
	fmt.Println("===Walk======")
	filepath.Walk(p1, func(path string, info fs.FileInfo, err error) error {
		fmt.Println(path, info.Name(), info.IsDir(), err) //递归读出目录和文件
		return err
	})
	fmt.Println("=====ioutil.ReadDir=====")
	fi, err := ioutil.ReadDir(p1)
	if err != nil {
		fmt.Println(err)
		return
	}
	//fmt.Println(fi)
	for i, v := range fi {
		fmt.Println(i, v.Name(), v.IsDir())      // 0 b true    1 e true
		fmt.Println(filepath.Join(p1, v.Name())) // /a/b    /a/e
	}
}
func main() {
	//example1()
	//example2()
	//example3()
	//example4()
	example5()

}