golang的filepath包函数示例

golang 专栏收录该内容
10 篇文章 0 订阅

func Abs(path string) (string, error)

package main
import (
    "path/filepath"
    "os"
    "fmt"
)

func main() {
    //
    pName := os.Args[0]
    absName, err := filepath.Abs(pName)
    if err != nil{
        fmt.Println(err)
    }

    fmt.Println(absName)
}

func Base(path string) string

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    baseName := filepath.Base("/a/b/c/e.txt")
    fmt.Println(baseName)
}

func Clean(path string) string

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    p := "../..//././//a/b/c.txt"
    pc := filepath.Clean(p)
    fmt.Println(pc)
}

func Dir(path string) string

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    d := filepath.Dir("/a/b/c/d.txt")
    fmt.Println(d)
}

func EvalSymlinks(path string) (string, error)

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    e, _:= filepath.EvalSymlinks("/Users/hyhu/SourcePrj/mysourceprj")
    fmt.Println(e)
}

func Ext(path string) string

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    e := filepath.Ext("/Users/1.txt")
    fmt.Println(e)
}

func FromSlash(path string) string

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    //windows下有效果
    r := filepath.FromSlash("/a//b/c/d.txt")
    fmt.Println(r)
}

func Glob(pattern string) (matches []string, err error)

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    m,_ := filepath.Glob("/usr/*")
    fmt.Println(m)
}

func HasPrefix(p, prefix string) bool

Go1.7中已经废弃使用

func IsAbs(path string) bool

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    b := filepath.IsAbs("/a/b/c/d.txt")
    fmt.Println(b)
    b = filepath.IsAbs("d.txt")
    fmt.Println(b)
}

func Join(elem …string) string

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    fmt.Println("On Unix:")
    fmt.Println(filepath.Join("a", "b", "c"))
    fmt.Println(filepath.Join("a", "b/c"))
    fmt.Println(filepath.Join("a/b", "c"))
    fmt.Println(filepath.Join("a/b", "/c"))
}

func Match(pattern, name string) (matched bool, err error)

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    //windows下有效果
    m,_ := filepath. Match("/usr/*", "/usr/local")
    fmt.Println(m)
}

func Rel(basepath, targpath string) (string, error)

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    paths := []string{
        "/a/b/c",
        "/b/c",
        "./b/c",
    }
    base := "/a"

    fmt.Println("On Unix:")
    for _, p := range paths {
        rel, err := filepath.Rel(base, p)
        fmt.Printf("%q: %q %v\n", p, rel, err)
    }

}

func Split(path string) (dir, file string)

package main

import (
    "fmt"
    "path/filepath"
)

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

func SplitList(path string) []string

package main

import (
    "fmt"
    "path/filepath"
)

func main() {
    fmt.Println("On Unix:", filepath.SplitList("/a/b/c:/usr/bin"))
}

func ToSlash(path string) string

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    //windows下有效果
    r := filepath.ToSlash("\\a\\b\\c/d.txt")
    fmt.Println(r)
}

func VolumeName(path string) string

windows平台下
C:\foo\bar 对应的结果 C:

\\host\share\foo 对应的结果 \\host\share

package main
import (
    "path/filepath"
    "fmt"
)

func main() {
    v := filepath.VolumeName("C:\foo\bar")
    fmt.Println(v)
    v = filepath.VolumeName(`\\host\share\foo"`)
    fmt.Println(v)
}

func Walk(root string, walkFn WalkFunc) error

同下

type WalkFunc

package main
import (
    "path/filepath"
    "fmt"
    "os"
)

func MyWalkFunc (path string, info os.FileInfo, err error) error{
    var e error

    fmt.Println(path, info.Name(), info.Size(), info.Mode(), info.ModTime())

    return e
}

func main() {
    filepath.Walk("/", MyWalkFunc)
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

白日梦想

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值