13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

创新互联GoFrame教程:GoFramegfile-路径操作

路径操作

Join

func Join(paths ...string) string 

func ExampleJoin() {
	// init
	var (
		dirPath  = gfile.TempDir("gfile_example_basic_dir")
		filePath = "file1"
	)

	// Joins string array paths with file separator of current system.
	joinString := gfile.Join(dirPath, filePath)

	fmt.Println(joinString)

	// Output:
	// /tmp/gfile_example_basic_dir/file1
}

Abs

func Abs(path string) string

func ExampleAbs() {
	// init
	var (
		path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
	)

	// Get an absolute representation of path.
	fmt.Println(gfile.Abs(path))

	// Output:
	// /tmp/gfile_example_basic_dir/file1
}

RealPath

func RealPath(path string) string

func ExampleRealPath() {
	// init
	var (
		realPath  = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
		worryPath = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "worryFile")
	)

	// fetch an absolute representation of path.
	fmt.Println(gfile.RealPath(realPath))
	fmt.Println(gfile.RealPath(worryPath))

	// Output:
	// /tmp/gfile_example_basic_dir/file1
	//
}

SelfName

func SelfName() string 

func ExampleSelfName() {

	// Get file name of current running process
	fmt.Println(gfile.SelfName())

	// May Output:
	// ___github_com_GOgf_gf_v2_os_gfile__ExampleSelfName
}

Basename

func Basename(path string) string

func ExampleBasename() {
	// init
	var (
		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
	)

	// Get the last element of path, which contains file extension.
	fmt.Println(gfile.Basename(path))

	// Output:
	// file.log
}

Name

func Name(path string) string

func ExampleName() {
	// init
	var (
		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
	)

	// Get the last element of path without file extension.
	fmt.Println(gfile.Name(path))

	// Output:
	// file
}

Dir

func Dir(path string) string 

func ExampleDir() {
	// init
	var (
		path = gfile.Join(gfile.TempDir("gfile_example_basic_dir"), "file1")
	)

	// Get all but the last element of path, typically the path's directory.
	fmt.Println(gfile.Dir(path))

	// Output:
	// /tmp/gfile_example_basic_dir
}

Ext

func Ext(path string) string

func ExampleExt() {
	// init
	var (
		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
	)

	// Get the file name extension used by path.
	fmt.Println(gfile.Ext(path))

	// Output:
	// .log
}

ExtName

func ExtName(path string) string

func ExampleExtName() {
	// init
	var (
		path = gfile.Pwd() + gfile.Separator + "testdata/readline/file.log"
	)

	// Get the file name extension used by path but the result does not contains symbol '.'.
	fmt.Println(gfile.ExtName(path))

	// Output:
	// log
}

MainPkgPath

func MainPkgPath() string

func Test() {
	fmt.Println("main pkg path on main :", gfile.MainPkgPath())
	char := make(chan int, 1)
	go func() {
		fmt.Println("main pkg path on goroutine :", gfile.MainPkgPath())
		char <- 1
	}()
	select {
	case <-char:
	}
	// Output:
	// /xxx/xx/xxx/xx
	// /xxx/xx/xxx/xx
}
// 二进制包
$ ./testDemo 
main pkg path on main : /xxx/xx/xxx/xx
main pkg path on goroutine : /xxx/xx/xxx/xx


网站栏目:创新互联GoFrame教程:GoFramegfile-路径操作
网站链接:http://cdbrznjsb.com/article/dphhojp.html

其他资讯

让你的专属顾问为你服务