Golang filepath.IsAbs()函数的使用示例
在Go语言中,path包用于以斜线分隔的路径,例如URL中的路径。Go语言的 filepath.IsAbs() 函数用于报告路径是否为绝对路径。此外,此函数定义在path包中。在这里,您需要导入”path/filepath”包才能使用这些函数。
语法:
func IsAbs(path string) bool
在这里,’path’是指定的绝对或非绝对路径。
返回值:
它返回 true 表示绝对路径,否则返回 false。
示例1:
//Golang程序,演示filepath.IsAbs()函数的用法
//包含主包
package main
//导入fmt和path/filepath
import (
"fmt"
"path/filepath"
)
//调用主函数
func main() {
//调用IsAbs()函数,使用一些绝对和非绝对路径
fmt.Println(filepath.IsAbs("/home/gfg"))
fmt.Println(filepath.IsAbs(".gfg"))
fmt.Println(filepath.IsAbs("/gfg"))
fmt.Println(filepath.IsAbs(":gfg"))
}
输出:
true
false
true
false
示例2:
//Golang程序,演示filepath.IsAbs()函数的用法
//包含主包
package main
//导入fmt和path/filepath
import (
"fmt"
"path/filepath"
)
//调用主函数
func main() {
//调用IsAbs()函数,使用一些绝对和非绝对路径
fmt.Println(filepath.IsAbs("/"))
fmt.Println(filepath.IsAbs("."))
fmt.Println(filepath.IsAbs(":"))
fmt.Println(filepath.IsAbs("/."))
fmt.Println(filepath.IsAbs("//"))
fmt.Println(filepath.IsAbs(":/"))
}
输出:
true
false
false
true
true
false
极客教程