Go 获取文件名,文件后缀和除去后缀
import (
"fmt"
"runtime"
"path"
"strings"
)
func main() {
_, 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)
}
测试过程和结果:
D:\tmp\tmp_dev_root\go\src\github.com\user\EmulateLoginBaidu>go run EmulateLoginBaidu.go
D:/tmp/tmp_dev_root/go/src/github.com/user/EmulateLoginBaidu/EmulateLoginBaidu.go
filenameWithSuffix= EmulateLoginBaidu.go
fileSuffix= .go
filenameOnly= EmulateLoginBaidu
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。