path package
sublime3不能输入中文解决办法(ubuntu)
$ git clone https://github.com/lyfeyaj/sublime-text-imfix.git
$ cd ~/sublime-text-imfix
$ sudo cp ./lib/libsublime-imfix.so /opt/sublime_text/
$ sudo cp ./src/subl /usr/bin/
$ subl
index
func Base(path string) string
/*
*path包的用法
*/
package main
import (
"fmt"
"os"
"path"
"runtime"
)
func main() {
//获取当前文件名全称(unix中文件名是从根目录开始的绝对路径,只有文件名没有路径只是该文件的简称)
_, file, _, _ := runtime.Caller(0)
fmt.Println("file: ", file)
// 获取当前路径
pwd, err := os.Getwd()
if err != nil {
fmt.Println("get pwd Error: ", err)
}
fmt.Println("pwd: ", pwd)
base_pwd := path.Base(pwd)
fmt.Println("Base_pwd: ", base_pwd)
base_file := path.Base(file)
fmt.Println("Base_file: ", base_file)
}
output
file: /home/mgo/src/mpath.go
pwd: /home/mgo/src
Base_pwd: src
Base_file: mpath.go
由以上可以看出Base()只是获取路径中末尾的目录名或者文件名
==Base returns the last element of path. Trailing slashes are removed before extracting the last element. If the path is empty, Base returns “.”. If the path consists entirely of slashes, Base returns “/”.==
func Clean(path string) string
官网code
paths := []string{
"a/c",
"a//c",
"a/c/.",
"a/c/b/..",
"/../a/c",
"/../a/b/../././/c",
"../a/b"
}
for _, p := range paths {
fmt.Printf("Clean(%q) = %q\n", p, path.Clean(p))
}
output
Clean("a/c") = "a/c"
Clean("a//c") = "a/c"
Clean("a/c/.") = "a/c"
Clean("a/c/b/..") = "a/c"
Clean("/../a/c") = "/a/c"
Clean("/../a/b/../././/c") = "/a/c"
Clean("../a/b") = "../a/b"
由上可以看出Clean()相当与把一个复杂的路径变成一个最简洁的路径,遵循以下规则:
1. 将多个斜线替换为一个
2. 消除.路径(代表这当前路径)
3. 消除路径内部和结尾的..
4. 替换/..为/
func Dir(path string) string
file := "/home/mgo/src/mpath.go"
fmt.Println("Dir: ", path.Dir(file))
output
Dir: /home/mgo/src
返回父目录
func Ext(path string) string
file := "/home/mgo/src/mpath.go"
fmt.Println("Ext: ", path.Ext(file))
output
Ext: .go
获取文件扩展名,没有则返回空字符串
func IsAbs(path string) bool
判断路径是否为绝对路径
func Join(elem …string) string
fmt.Println(path,Join("a", "b", "c"))
fmt.Println(path,Join("a", "//b", "c"))
fmt.Println(path,Join("a", "//b", "../c"))
output
a/b/c
a/b/c
a/c
在组合文件名和路径时不用管烦人的斜线了,含有Clean()功能
func Match(pattern, name string) (matched bool, err error)
类似一个正则表达是但又有点不一样点
1. ““,”?”这两个和grep命令中表示的意思是一样的,”“匹配任意多个字符0次或多次,”?”匹配一个字符0次或多次
func Split(path string) (dir, file string)
path1 := "/home/mgo/src"
dir, name := path.Split(path1)
fmt.Println(dir, name)
path2 := "/home/mgo/src/mpath.go"
dir, name = path.Split(path1)
fmt.Println(dir, name)
output
/home/mgo src
/home/mgo/src mpath.go
和shell中的${path##/}和${path%/}的功能是一样的