golang package path

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/youshijian99/article/details/85686147

package filepath
filepath包实现了兼容各操作系统的文件路径的实用操作函数。
import “path/filepath”

func Join(elem ...string) string

Join函数可以将任意数量的路径元素放入一个单一路径里,会根据需要添加路径分隔符。结果是经过简化的,所有的空字符串元素会被忽略。

package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	home := os.Getenv("HOME")
	fmt.Println(home)
	tmp := filepath.Join(home, ".tttttt")
	fmt.Println(home)
	fmt.Println(tmp)
}

运行结果:
/root
/root
/root/.tttttt


func IsAbs(path string) bool

IsAbs返回路径是否是一个绝对路径。


func Base(path string) string

Base函数返回路径的最后一个元素。在提取元素前会求掉末尾的路径分隔符。如果路径是"",会返回".";如果路径是只有一个斜杆构成,会返回单个路径分隔符。

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	fmt.Println(filepath.Base("/root/gocode/src/github.com/geth.ipc"))
}

运行结果:
geth.ipc

猜你喜欢

转载自blog.csdn.net/youshijian99/article/details/85686147