Skip to content

Instantly share code, notes, and snippets.

@xin053
Created September 4, 2019 03:42
Show Gist options
  • Save xin053/77269ac82852e7047a33b7679b9c0d3c to your computer and use it in GitHub Desktop.
Save xin053/77269ac82852e7047a33b7679b9c0d3c to your computer and use it in GitHub Desktop.
[go 标准库 path/filepath] go 标准库 path/filepath #go #标准库 #path/filepath

func Ext(path string) string

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	fmt.Printf("No dots: %q\n", filepath.Ext("index")) // No dots: ""
	fmt.Printf("One dot: %q\n", filepath.Ext("index.js")) // One dot: ".js"
	fmt.Printf("Two dots: %q\n", filepath.Ext("main.test.js")) // Two dots: ".js"
}

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 (
	"fmt"
	"path/filepath"
)

func main() {
	fmt.Println("On Unix:")
	fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo"))
	fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo/bar"))
	fmt.Println(filepath.Match("/home/?opher", "/home/gopher"))
	fmt.Println(filepath.Match("/home/\\*", "/home/*"))

}

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"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment