Mar*_*arc 6

filepath.Base
package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    paths := []string{
        "/home/arnie/amelia.jpg",
        "/mnt/photos/",
        "rabbit.jpg",
        "/usr/local//go",
    }
    for _, p := range paths {
        dir := filepath.Dir(p)
        parent := filepath.Base(dir)
        fmt.Printf("input: %q\n\tdir: %q\n\tparent: %q\n", p, dir, parent)
    }
}

返回:

input: "/home/arnie/amelia.jpg"
    dir: "/home/arnie"
    parent: "arnie"
input: "/mnt/photos/"
    dir: "/mnt/photos"
    parent: "photos"
input: "rabbit.jpg"
    dir: "."
    parent: "."
input: "/usr/local//go"
    dir: "/usr/local"
    parent: "local"

(从文件路径示例改编的示例)