后浪云GoFrame教程:GoFrame gfile-权限操作

权限操作

IsReadable

  • 说明:检查给定的路径是否可读。
  • 格式: 
func IsReadable(path string) bool 

  • 示例:
func ExampleIsReadable() {
	// init
	var (
		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
	)

	// Checks whether given `path` is readable.
	fmt.Println(gfile.IsReadable(path))

	// Output:
	// true
}

IsWritable

  • 说明:检查指定路径是否可写,如果路径是目录,则会创建临时文件检查是否可写,如果是文件则判断是否可以打开
  • 格式: 
func IsWritable(path string) bool 

  • 示例:
func ExampleIsWritable() {
	// init
	var (
		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
	)

	// Checks whether given `path` is writable.
	fmt.Println(gfile.IsWritable(path))

	// Output:
	// true
}

Chmod

  • 说明:使用指定的权限,更改指定路径的文件权限。
  • 格式: 
func Chmod(path string, mode os.FileMode) error

  • 类型:
func ExampleChmod() {
	// init
	var (
		path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
	)

	// Get a FileInfo describing the named file.
	stat, err := gfile.Stat(path)
	if err != nil {
		fmt.Println(err.Error())
	}
	// Show original mode
	fmt.Println(stat.Mode())

	// Change file model
	gfile.Chmod(path, gfile.DefaultPermCopy)

	// Get a FileInfo describing the named file.
	stat, _ = gfile.Stat(path)
	// Show the modified mode
	fmt.Println(stat.Mode())

	// Output:
	// -rw-r--r--
	// -rwxrwxrwx
}

THE END