Is it possible to get the current root of package structure as a string in golang test?

前端 未结 5 2144
傲寒
傲寒 2021-01-31 08:02

I am writing a utility function for my unit tests which is used by unit tests in multiple packages. This utility function must read a particular file (always the same file). Her

5条回答
  •  感动是毒
    2021-01-31 08:34

    You can also use my method without C:

    package mypackage
    
    import (
        "path/filepath"
        "runtime"
        "fmt"
    )
    
    var (
        _, b, _, _ = runtime.Caller(0)
        basepath   = filepath.Dir(b)
    )
    
    func PrintMyPath() {
        fmt.Println(basepath)
    }
    

    https://play.golang.org/p/ifVRIq7Tx0

提交回复
热议问题