|
| 1 | +# Project Root Directory |
| 2 | + |
| 3 | +# 一、为什么会有这个东西? |
| 4 | + |
| 5 | +在Golang的单元测试`foo_test.go`中读取文件和在`main.go`中读取文件的初始路径根本就不一样,如果你使用的是相对路径读取配置文件之类的,就可能会被恶心到,此项目就是解决类似问题的,提供一个固定的项目根目录的绝对路径,你可以从这里出发去读取你的文件,为从不同地方运行访问文件都提供一致性的行为。 |
| 6 | + |
| 7 | +# 二、安装 |
| 8 | + |
| 9 | +```bash |
| 10 | +go get -u https://github.com/golang-infrastructure/go-project-root-directory |
| 11 | +``` |
| 12 | + |
| 13 | +# 三、示例 |
| 14 | + |
| 15 | +## 3.1 获取项目的根目录 |
| 16 | + |
| 17 | +在main.go中: |
| 18 | + |
| 19 | +```go |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + project_root_directory "github.com/golang-infrastructure/go-project-root-directory" |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + |
| 29 | + directory, err := project_root_directory.GetRootDirectory() |
| 30 | + if err != nil { |
| 31 | + fmt.Println(err.Error()) |
| 32 | + return |
| 33 | + } |
| 34 | + fmt.Println(directory) |
| 35 | + // Output: |
| 36 | + // D:\workspace\go-project-root-directory |
| 37 | + |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +在单元测试中,获取到的路径都是相同的: |
| 42 | + |
| 43 | +```go |
| 44 | +package test |
| 45 | + |
| 46 | +import ( |
| 47 | + project_root_directory "github.com/golang-infrastructure/go-project-root-directory" |
| 48 | + "github.com/stretchr/testify/assert" |
| 49 | + "testing" |
| 50 | +) |
| 51 | + |
| 52 | +func Test_foo(t *testing.T) { |
| 53 | + directory, err := project_root_directory.GetRootDirectory() |
| 54 | + assert.Nil(t, err) |
| 55 | + t.Log(directory) |
| 56 | + // Output: |
| 57 | + // D:\workspace\go-project-root-directory |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## 3.2 获取项目根路径下的文件的路径 |
| 62 | + |
| 63 | +```go |
| 64 | +package main |
| 65 | + |
| 66 | +import ( |
| 67 | + "fmt" |
| 68 | + project_root_directory "github.com/golang-infrastructure/go-project-root-directory" |
| 69 | +) |
| 70 | + |
| 71 | +func main() { |
| 72 | + |
| 73 | + path, err := project_root_directory.GetRootFilePath("go.mod") |
| 74 | + if err != nil { |
| 75 | + fmt.Println(err.Error()) |
| 76 | + return |
| 77 | + } |
| 78 | + fmt.Println(path) |
| 79 | + // Output: |
| 80 | + // D:\workspace\go-project-root-directory\go.mod |
| 81 | + |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## 3.3 读取项目根路径下的文件的内容 |
| 86 | + |
| 87 | +```go |
| 88 | +package main |
| 89 | + |
| 90 | +import ( |
| 91 | + "fmt" |
| 92 | + project_root_directory "github.com/golang-infrastructure/go-project-root-directory" |
| 93 | +) |
| 94 | + |
| 95 | +func main() { |
| 96 | + |
| 97 | + file, err := project_root_directory.ReadRootFile("go.mod") |
| 98 | + if err != nil { |
| 99 | + fmt.Println(err.Error()) |
| 100 | + return |
| 101 | + } |
| 102 | + fmt.Println(string(file)) |
| 103 | + // Output: |
| 104 | + // module github.com/golang-infrastructure/go-project-root-directory |
| 105 | + // |
| 106 | + // go 1.19 |
| 107 | + // |
| 108 | + // require ( |
| 109 | + // github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748 |
| 110 | + // github.com/stretchr/testify v1.8.1 |
| 111 | + // ) |
| 112 | + // |
| 113 | + // require ( |
| 114 | + // github.com/davecgh/go-spew v1.1.1 // indirect |
| 115 | + // github.com/pmezard/go-difflib v1.0.0 // indirect |
| 116 | + // gopkg.in/yaml.v3 v3.0.1 // indirect |
| 117 | + // ) |
| 118 | + |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
0 commit comments