文章目录 常量(Constants) 变量(Variables) 常用函数 文件操作
- 1. Chdir()
- 2. Chmod()
- 3. Chown()
- 4. Chtimes()
- 5. Clearenv()
- 6. Environ()
- 7. Executable()
- 8. Exit()
- 9. Expand()
- 10. ExpandEnv()
- 11. Geteuid()
- 12. Getuid()
- 13. Getegid()
- 14. Getgid()
- 15. Getenv()
- 16. Getgroups()
- 17. Getpagesize()
- 18. Getpid()
- 19. Getppid()
- 20. Getwd()
- 21. Hostname()
- 22. IsExist()
- 23. IsNotExist()
- 24. IsPathSeparator()
- 25. IsPermission()
- 26. Lchown()
- 27. Link()
- 28. LookupEnv()
- 29. Mkdir()
- 30. MkdirAll()
- 31. NewSyscallError()
- 32. Readlink()
- 33. Remove()
- 34. RemoveAll()
- 35. Rename()
- 36. SameFile()
- 37. Symlink()
- 38. TempDir()
- 39. Truncate()
- 40. Unsetenv()
FileMode
常量(Constants)
const (
O_RDONLY int = syscall.O_RDONLY // 值为0,以只读的方式打开
O_WRONLY int = syscall.O_WRONLY // 值为1,以只写的方式打开
O_RDWR int = syscall.O_RDWR // 值为2,以读写的方式打开
O_APPEND int = syscall.O_APPEND // 值为1024,以追加的方式写入
O_CREATE int = syscall.O_CREAT // 值为64,如果文件不存在,将会新建
O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist
O_SYNC int = syscall.O_SYNC // open for synchronous I/O.
O_TRUNC int = syscall.O_TRUNC // if possible, truncate file when opened.
)
const (
SEEK_SET int = 0 // seek relative to the origin of the file
SEEK_CUR int = 1 // seek relative to the current offset
SEEK_END int = 2 // seek relative to the end
)
const (
PathSeparator = '\' // OS-specific path separator
PathListSeparator = ';' // OS-specific path list separator
)
变量(Variables)
var (
ErrInvalid = errors.New("invalid argument") // methods on File will return this error when the receiver is nil
ErrPermission = errors.New("permission denied")
ErrExist = errors.New("file already exists")
ErrNotExist = errors.New("file does not exist")
ErrClosed = errors.New("file already closed")
)
var (
Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")
)
var Args []string
常用函数
Chdir()
func Chdir(dir string) error
package main
import "os"
func main() {
os.Chdir("../")
os.Chdir("home/hxxz")
}
Chmod()
func Chmod(name string, mode FileMode) error
Chown()
func Chown(name string, uid, gid int) error
Chtimes()
func Chtimes(name string, atime time.Time, mtime time.Time) error
Clearenv()
func Clearenv()
Environ()
func Environ() []string
fmt.Println(os.Environ())
Executable()
func Executable() (string, error)
Exit()
func Exit(code int)
Expand()
func Expand(s string, mapping func(string) string) string
mapping := func(s string) string {
m := map[string]string{"str": "我会替换$str变量", "hello": "我会替换$hello变量"}
return m[s]
}
str := os.Expand("你好世界 $str $hello world", mapping) //替换字符串中的$xxx 变量
fmt.Println(str) //你好世界 我会替换$str变量 我会替换$hello变量 world
path := os.ExpandEnv("gopath is $GOPATH") //替换字符串中的环境变量
fmt.Println(path) //gopath is /wide/gogogo
ExpandEnv()
func ExpandEnv(s string) string
Geteuid()
func Geteuid() int
Getuid()
func Geteuid() int
Getegid()
func Getegid() int
Getgid()
func Getegid() int
Getenv()
func Getenv(key string) string
Getgroups()
func Getgroups() ([]int, error)
Getpagesize()
func Getpagesize() int
Getpid()
func Getpid() int
Getppid()
func Getppid() int
Getwd()
func Getwd() (dir string, err error)
Hostname()
func Hostname() (name string, err error)
IsExist()
func IsExist(err error) bool
err := os.Mkdir("/hmoe", 0755)
fmt.Println(os.IsExist(err)) //true
IsNotExist()
func IsNotExist(err error) bool
IsPathSeparator()
func IsPathSeparator(c uint8) bool
IsPermission()
func IsPermission(err error) bool
Lchown()
func Lchown(name string, uid, gid int) error
Link()
func Link(oldname, newname string) error
LookupEnv()
func LookupEnv(key string) (string, bool)
//如果有环境变量v,内容为空
fmt.Println(os.Getenv("v")) //返回`空`
fmt.Println(os.LookupEnv("v")) //俩返回值,会返回`空`以及`true`
//如果不存在环境变量n
fmt.Println(os.Getenv("n")) //返回`空`
fmt.Println(os.LookupEnv("n")) //俩返回值,会返回`空`以及`false`
Mkdir()
func Mkdir(name string, perm FileMode) error
MkdirAll()
func MkdirAll(path string, perm FileMode) error
NewSyscallError()
func NewSyscallError(syscall string, err error) error
Readlink()
func Readlink(name string) (string, error)
Remove()
func Remove(name string) (string, error)
RemoveAll()
func RemoveAll(path string) error
Rename()
func Rename(oldpath, newpath string) error
err := os.Rename("main.go1", "main.go2")
err1 := os.Rename("pkg2", "pkg2.new")
err2 := os.Rename("../1.go", "2.go") // 这里会将1.go从上级目录直接移动到本级目录并重命名为2.go
err3 := os.Rename("/home/2.go2", "../1.go")
SameFile()
func SameFile(fi1, fi2 FileInfo) bool
Symlink()
func Symlink(oldname, newname string) error
TempDir()
func TempDir() string
Truncate()
func Truncate(name string, size int64) error
Unsetenv()
func Unsetenv(key string) error
文件操作
type File struct {
*file // 定义struct类型的File文件,返回File的地址
}
type file struct {
fd int
name string
dirinfo *dirInfo // nil unless directory being read
}
—file