简介
本文主要做一个知识积累,在做项目的过程中,绝对路径转相对路径的时候,一下子有点懵了。
本文的主角是 go
的官方库 path/filepath
。
判断是否为绝对路径
func IsAbs(path string) (b bool)
fmt.Println(filepath.IsAbs("master.exe")) // false
绝对路径转相对路径
func Rel(basepath, targpath string) (string, error)
fmt.Println(filepath.Rel("", "")) // . <nil>
相对路径转绝对路径
func Abs(path string) (string, error)
fmt.Println(filepath.Abs("./master.exe")) // F:\GoTest\GoTest\master.exe <nil>