Go 代码

    1. package main
    2. import (
    3. "fmt"
    4. "os"
    5. "strings"
    6. )
    7. // 从文件路径中取得目录路径
    8. func PathToDir(path string) string {
    9. if len(path) < 1 {
    10. return path
    11. }
    12. pathRune := []rune(path)
    13. if os.IsPathSeparator(uint8(pathRune[len(pathRune)-1])) {
    14. pathRune = pathRune[len(pathRune)-1:]
    15. }
    16. path = string(pathRune)
    17. tmp := strings.Split(path, string(os.PathSeparator))
    18. newPath := strings.Join(tmp[:len(tmp)-1], string(os.PathSeparator))
    19. return newPath
    20. }
    21. func main() {
    22. dir := PathToDir("/etc/cron.d/e2scrub_all")
    23. fmt.Println(dir)
    24. }

    输出

    1. /etc/cron.d