
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
github.com/golang-infrastructure/go-project-root-directory
在Golang的单元测试foo_test.go
中读取文件和在main.go
中读取文件的初始路径根本就不一样,如果你使用的是相对路径读取配置文件之类的,就可能会被恶心到,此项目就是解决类似问题的,提供一个固定的项目根目录的绝对路径,你可以从这里出发去读取你的文件,为从不同地方运行访问文件都提供一致性的行为。
go get -u https://github.com/golang-infrastructure/go-project-root-directory
在main.go中:
package main
import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)
func main() {
directory, err := project_root_directory.GetRootDirectory()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(directory)
// Output:
// D:\workspace\go-project-root-directory
}
在单元测试中,获取到的路径都是相同的:
package test
import (
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
"github.com/stretchr/testify/assert"
"testing"
)
func Test_foo(t *testing.T) {
directory, err := project_root_directory.GetRootDirectory()
assert.Nil(t, err)
t.Log(directory)
// Output:
// D:\workspace\go-project-root-directory
}
package main
import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)
func main() {
path, err := project_root_directory.GetRootFilePath("go.mod")
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(path)
// Output:
// D:\workspace\go-project-root-directory\go.mod
}
package main
import (
"fmt"
project_root_directory "github.com/golang-infrastructure/go-project-root-directory"
)
func main() {
file, err := project_root_directory.ReadRootFile("go.mod")
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(string(file))
// Output:
// module github.com/golang-infrastructure/go-project-root-directory
//
// go 1.19
//
// require (
// github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748
// github.com/stretchr/testify v1.8.1
// )
//
// require (
// github.com/davecgh/go-spew v1.1.1 // indirect
// github.com/pmezard/go-difflib v1.0.0 // indirect
// gopkg.in/yaml.v3 v3.0.1 // indirect
// )
}
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.