最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】go语言中实现获得当前文件的文件名

GO crifan 9148浏览 0评论

【背景】

折腾:

【已解决】go语言中实现输出内容到log文件

期间,想要获得当前go文件的文件名,作为日志文件的文件名。

【折腾过程】

1.参考:

compile-time info, such as line number and file name

貌似通过runtime是可以的。

所以去试试:

    _, filename, line, _ := runtime.Caller(0)
    fmt.Println(filename)
    fmt.Println(line)

结果是:

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模块:

Package os

中,也没有相关的功能可以得到当前文件名的。

不过看到一个:

func Getwd

func Getwd

func 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.

所以去试试:

    var currentDir string
    currentDir = os.Getwd()
    fmt.Println("currentDir=%s", currentDir)

结果是:

出错:

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.注意返回值是两个,所以再去改为:

    var currentDir string
    currentDir, err = os.Getwd()
    fmt.Println("currentDir=%s", currentDir)

结果是:

出错:

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.再去改为:

    var currentDir string
    currentDir, err := os.Getwd()
    fmt.Println("currentDir=%s", currentDir)

结果是:

仍旧出错:

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.再去改为:

    var currentDir string
    currentDir, err := os.Getwd()
    fmt.Println("currentDir=%s", currentDir)
    if err != nil {
        fmt.Println("get current directory error=%s\n", err)
    }

结果是:

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去得到当前文件的全路径,然后再去处理此字符串,得到文件名吧。

不过看到了:

Package path

中有一堆相关函数:

func Base(path string) string
func Clean(path string) string
func Dir(path string) string
func Ext(path string) string
func IsAbs(path string) bool
func Join(elem …string) string
func Match(pattern, name string) (matched bool, err error)
func Split(path string) (dir, file string)

所以,可以去利用这些函数,对于从runtime.Caller得到的文件全路径处理,而得到当前文件名。

去试试。

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)
}

结果是:

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,即得到文件名本身。

本来打算去用字符串分割的,后来发现有:

func Ext

是可以获得后缀名的。

所以去试试:

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)
}

结果是:

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

的最右边,去除掉。

从而得到文件名本身:

【已解决】go语言中字符串的分割和替换:去掉文件名中的后缀

至此,达到我们的目标了:

获得当前的xxx.go文件的文件名xxx。

remove suffix from filename

 

【总结】

想要获得当前文件名,可以通过:

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)
}

输出为:

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语言中实现获得当前文件的文件名

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.167 seconds, using 22.11MB memory