GoFrame gfile-权限操作

2022-04-09 11:35 更新

权限操作

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
}


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号