golang의 filepath 패키지 함수 예시

5490 단어

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
", 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
\tdir: %q
\tfile: %q
", 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


윈도우즈 플랫폼에서 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)
}

좋은 웹페이지 즐겨찾기