读取目录
package main
import (
"fmt"
"os"
)
func main() {
dirs, err := os.ReadDir("./search")
if err != nil {
fmt.Println(err)
}
for _, dir := range dirs {
fileinfo, _ := dir.Info()
fmt.Println(fileinfo)
fmt.Println("is_dir:", dir.IsDir(), "name:", dir.Name())
}
}
切换目录
package main
import (
"fmt"
"os"
)
func main() {
currentDir, _ := os.Getwd()
fmt.Println("current_dir:", currentDir) // current_dir: D:\Project\test_go\test
err := os.Chdir("./search")
if err != nil {
fmt.Println(err)
}
currentDir, _ = os.Getwd()
fmt.Println("current_dir:", currentDir) // current_dir: D:\Project\test_go\test\search
}
修改文件或目录权限
package main
import (
"fmt"
"os"
)
var FileMode os.FileMode = 0400 // go的Windows权限模式不同于Linux, 0200和0600读写,0400只读
func main() {
const FilePath = "./search/default.go"
fileinfo, _ := os.Stat(FilePath)
fmt.Println(fileinfo.Mode()) // -rw-rw-rw-
err := os.Chmod(FilePath, FileMode)
if err != nil {
fmt.Println(err)
}
fileinfo, _ = os.Stat(FilePath)
fmt.Println(fileinfo.Mode()) // -r--r--r--
}
package main
import (
"fmt"
"os"
)
var FileMode os.FileMode = 0400 // go的Windows权限模式不同于Linux, 0200和0600读写,0400只读
func main() {
const FilePath = "./search"
fileinfo, _ := os.Stat(FilePath)
fmt.Println(fileinfo.Mode()) // drwxrwxrwx
err := os.Chmod(FilePath, FileMode)
if err != nil {
fmt.Println(err)
}
fileinfo, _ = os.Stat(FilePath)
fmt.Println(fileinfo.Mode()) // dr-xr-xr-x
}
创建目录
package main
import (
"log"
"os"
)
var DirMode os.FileMode = 0755
func main() {
err := os.Mkdir("test", DirMode)
if err != nil {
log.Print(err)
}
}
递归创建目录
package main
import (
"log"
"os"
)
var DirMode os.FileMode = 0755
func main() {
err := os.MkdirAll("test/test/test", DirMode)
if err != nil {
log.Print(err)
}
}
删除目录或文件
package main
import (
"log"
"os"
)
func main() {
err := os.Remove("test/test/test")
if err != nil {
log.Print(err)
}
}
递归删除目录
package main
import (
"log"
"os"
)
func main() {
err := os.RemoveAll("test")
if err != nil {
log.Print(err)
}
}
目录重命名
package main
import (
"log"
"os"
)
func main() {
oldName := "test"
newName := "dir"
err := os.Rename(oldName, newName)
if err != nil {
log.Fatal(err)
}
}
读取文件
package main
import (
"fmt"
"os"
)
func main() {
data, err := os.ReadFile("test.txt")
if err != nil {
fmt.Println(err)
}
fmt.Println(string(data)) // hello world
}
写文件
package main
import (
"fmt"
"io/fs"
"os"
)
func main() {
err := os.WriteFile("test.txt", []byte("hello world"), fs.ModeType)
if err != nil {
fmt.Println(err)
}
}
创建文件
用Create()函数创建一个名为name的文件,默认采用模式0666。如果文件已存在,则它会被重置为空文件。如果成功,则返回的文件描述符对象可用于文件的读写。
package main
import (
"fmt"
"os"
)
func main() {
fp, err := os.Create("./test.txt")
if err != nil {
fmt.Println("file create failed")
}
fmt.Printf("%T", fp) // *os.File
defer fp.Close()
}
获取路径的绝对路径
package main
import (
"fmt"
"path/filepath"
)
func main() {
f, _ := filepath.Abs("./search")
fmt.Println(f) // D:\Project\test_go\test\search
}
路径拼接
package main
import (
"fmt"
"path/filepath"
)
func main() {
f := filepath.Join("test", "search", "default.go")
fmt.Println(f) // test\search\default.go
}
返回路径 basename
package main
import (
"fmt"
"path/filepath"
)
func main() {
f := filepath.Base("D:\\Project\\test_go\\test\\search")
fmt.Println(f) // search
}
返回路径的简洁模式
package main
import (
"fmt"
"path/filepath"
)
func main() {
f := filepath.Clean("./search/../data")
fmt.Println(f) // search
}
返回路径的上级目录
package main
import (
"fmt"
"path/filepath"
)
func main() {
f := filepath.Dir("./search/default.go")
fmt.Println(f) // search
}
返回文件的扩展名
package main
import (
"fmt"
"path/filepath"
)
func main() {
f := filepath.Ext("./search/default.go")
fmt.Println(f) // .go
}
路径使用系统兼容的路径连接符
package main
import (
"fmt"
"path/filepath"
)
func main() {
f := filepath.FromSlash("./search/default.go")
fmt.Println(f) // .\search\default.go
}
获取路径中的目录和文件
package main
import (
"fmt"
"path/filepath"
)
func main() {
dir, file := filepath.Split("./search/default.go")
fmt.Println(dir) // ./search/
fmt.Println(file) // default.go
}
通配文件
package main
import (
"fmt"
"path/filepath"
)
func main() {
files, _ := filepath.Glob("./search/*.go")
for _, file := range files {
fmt.Println(file)
}
// search\default.go
// search\feed.go
// search\match.go
// search\search.go
}
绝对路径判断
package main
import (
"fmt"
"path/filepath"
)
func main() {
isAbs := filepath.IsAbs("./search/match.go")
fmt.Println("isAbsDir:", isAbs) // isAbsDir: false
abspath, _ := filepath.Abs("./search/match.go")
fmt.Println(abspath) // D:\MyProject\Go\test\search\match.go
isAbs = filepath.IsAbs("D:\\MyProject\\Go\\test\\search\\match.go")
fmt.Println("isAbsDir:", isAbs) // isAbsDir: true
}
目录遍历
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
root := `./test`
err := filepath.Walk(root, scan)
if err != nil {
fmt.Printf("filepath.Walk() return %v\n", err)
//scaned dir: ./test
//scaned dir: test\test
//scaned dir: test\test\test
//scaned file: test\test\test\test.txt
}
}
func scan(path string, f os.FileInfo, err error) error {
if f.IsDir() {
fmt.Printf("scaned dir: %s\n", path)
} else {
fmt.Printf("scaned file: %s\n", path)
}
return nil
}