【背景】
折腾:
期间,想要获得当前go文件的文件名,作为日志文件的文件名。
【折腾过程】
1.参考:
compile-time info, such as line number and file name
貌似通过runtime是可以的。
所以去试试:
1 2 3 | _, filename, line, _ := runtime.Caller( 0 ) fmt.Println(filename) fmt.Println(line) |
结果是:
1 2 3 4 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go this is EmulateLoginBaidu.go D:/tmp/tmp_dev_root/go/src/github.com/user/EmulateLoginBaidu/EmulateLoginBaidu.go 15 |
但是觉得不是很好的感觉。
2.但是找了半天,貌似对应os模块:
中,也没有相关的功能可以得到当前文件名的。
不过看到一个:
func Getwdfunc Getwd() (pwd string, err error) Getwd returns a rooted path name corresponding to the current directory. If the current directory can be reached via multiple paths (due to symbolic links), Getwd may return any one of them. |
所以去试试:
1 2 3 | var currentDir string currentDir = os.Getwd() fmt.Println( "currentDir=%s" , currentDir) |
结果是:
出错:
1 2 3 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go # command-line-arguments .\EmulateLoginBaidu.go:20: multiple-value os.Getwd() in single-value context |
3.注意返回值是两个,所以再去改为:
1 2 3 | var currentDir string currentDir, err = os.Getwd() fmt.Println( "currentDir=%s" , currentDir) |
结果是:
出错:
1 2 3 4 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go # command-line-arguments .\EmulateLoginBaidu.go:20: undefined: err .\EmulateLoginBaidu.go:20: cannot assign to err |
4.再去改为:
1 2 3 | var currentDir string currentDir, err := os.Getwd() fmt.Println( "currentDir=%s" , currentDir) |
结果是:
仍旧出错:
1 2 3 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go # command-line-arguments .\EmulateLoginBaidu.go:20: err declared and not used |
5.再去改为:
1 2 3 4 5 6 | var currentDir string currentDir, err := os.Getwd() fmt.Println( "currentDir=%s" , currentDir) if err != nil { fmt.Println( "get current directory error=%s\n" , err) } |
结果是:
1 2 3 4 5 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go this is EmulateLoginBaidu.go D:/tmp/tmp_dev_root/go/src/github.com/user/EmulateLoginBaidu/EmulateLoginBaidu.go 15 currentDir=%s D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu |
算是终于可以正常获得当前文件夹了。
但是注意到:
os.Getwd所得到的路径是,反斜杠的;
而runtime.Caller得到的路径是斜杠的;
所以不能直接去将两者,以字符串去比较处理,否则肯定不相等。
6.算了,还是用runtime.Caller去得到当前文件的全路径,然后再去处理此字符串,得到文件名吧。
不过看到了:
中有一堆相关函数:
所以,可以去利用这些函数,对于从runtime.Caller得到的文件全路径处理,而得到当前文件名。
去试试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package main import ( "fmt" //"log" //"os" //"io/ioutil" //"net/http" "runtime" "path" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) _, fulleFilename, line, _ := runtime.Caller( 0 ) fmt.Println(fulleFilename) fmt.Println(line) var filename string filename = path.Base(fulleFilename) fmt.Println( "filename=" , filename) } |
结果是:
1 2 3 4 5 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go this is EmulateLoginBaidu.go D:/tmp/tmp_dev_root/go/src/github.com/user/EmulateLoginBaidu/EmulateLoginBaidu.go 16 filename= EmulateLoginBaidu.go |
即,可以得到文件名了。
7.所以,再去去掉.go,即得到文件名本身。
本来打算去用字符串分割的,后来发现有:
是可以获得后缀名的。
所以去试试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package main import ( "fmt" //"log" //"os" //"io/ioutil" //"net/http" "runtime" "path" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) _, fulleFilename, line, _ := runtime.Caller( 0 ) fmt.Println(fulleFilename) fmt.Println(line) var filename string filename = path.Base(fulleFilename) fmt.Println( "filename=" , filename) var fileSuffix string fileSuffix = path.Ext(filename) fmt.Println( "fileSuffix=" , fileSuffix) } |
结果是:
1 2 3 4 5 6 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go this is EmulateLoginBaidu.go D:/tmp/tmp_dev_root/go/src/github.com/user/EmulateLoginBaidu/EmulateLoginBaidu.go 16 filename= EmulateLoginBaidu.go fileSuffix= .go |
8.然后,再想着,如何把:
.go
从:
EmulateLoginBaidu.go
的最右边,去除掉。
从而得到文件名本身:
至此,达到我们的目标了:
获得当前的xxx.go文件的文件名xxx。
【总结】
想要获得当前文件名,可以通过:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package main import ( "fmt" "runtime" "path" "strings" ) func main() { fmt.Printf( "this is EmulateLoginBaidu.go\n" ) _, fulleFilename, line, _ := runtime.Caller( 0 ) fmt.Println(fulleFilename) fmt.Println(line) var filenameWithSuffix string filenameWithSuffix = path.Base(fulleFilename) fmt.Println( "filenameWithSuffix=" , filenameWithSuffix) var fileSuffix string fileSuffix = path.Ext(filenameWithSuffix) fmt.Println( "fileSuffix=" , fileSuffix) var filenameOnly string filenameOnly = strings.TrimSuffix(filenameWithSuffix, fileSuffix) fmt.Println( "filenameOnly=" , filenameOnly) } |
输出为:
1 2 3 4 5 6 7 | D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go this is EmulateLoginBaidu.go D:/tmp/tmp_dev_root/go/src/github.com/user/EmulateLoginBaidu/EmulateLoginBaidu.go 17 filenameWithSuffix= EmulateLoginBaidu.go fileSuffix= .go filenameOnly= EmulateLoginBaidu |
即可。
转载请注明:在路上 » 【已解决】go语言中实现获得当前文件的文件名