Go 代码
package main
import (
"fmt"
"os"
"strings"
)
// 从文件路径中取得目录路径
func PathToDir(path string) string {
if len(path) < 1 {
return path
}
pathRune := []rune(path)
if os.IsPathSeparator(uint8(pathRune[len(pathRune)-1])) {
pathRune = pathRune[len(pathRune)-1:]
}
path = string(pathRune)
tmp := strings.Split(path, string(os.PathSeparator))
newPath := strings.Join(tmp[:len(tmp)-1], string(os.PathSeparator))
return newPath
}
func main() {
dir := PathToDir("/etc/cron.d/e2scrub_all")
fmt.Println(dir)
}
输出
/etc/cron.d