GOLANG获取上上层目录
在Golang中,可以使用filepath.Dir()函数来获取上层目录。如果要获取上上层目录,可以多次调用filepath.Dir()函数。下面是一个示例代码:
package main
import (
"fmt"
"path/filepath"
)
func main() {
currentDir, _ := filepath.Abs(".")
parentDir := filepath.Dir(currentDir)
grandparentDir := filepath.Dir(parentDir)
fmt.Println(grandparentDir)
}
上面的代码中,filepath.Abs()函数用于获取当前目录的绝对路径。然后,通过多次调用filepath.Dir()函数获取上上层目录的路径。最后,使用fmt.Println()函数打印出上上层目录的路径。
注意:在实际使用中,应该对错误进行处理,以避免出现潜在的问题。上面的示例中,为了简化代码,省略了错误处理部分。
原文地址: http://www.cveoy.top/t/topic/iJY6 著作权归作者所有。请勿转载和采集!