project_root_directory

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2023 License: MIT Imports: 4 Imported by: 3

README

Project Root Directory

一、为什么会有这个东西?

在Golang的单元测试foo_test.go中读取文件和在main.go中读取文件的初始路径根本就不一样,如果你使用的是相对路径读取配置文件之类的,就可能会被恶心到,此项目就是解决类似问题的,提供一个固定的项目根目录的绝对路径,你可以从这里出发去读取你的文件,为从不同地方运行访问文件都提供一致性的行为。

二、安装

go get -u https://github.com/golang-infrastructure/go-project-root-directory

三、示例

3.1 获取项目的根目录

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

3.2 获取项目根路径下的文件的路径

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

}

3.3 读取项目根路径下的文件的内容

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

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrProjectRootDirectoryUnknown = errors.New("project root directory is unknown")
)

Functions

func GetExecutableRootDirectory

func GetExecutableRootDirectory() (string, error)

GetExecutableRootDirectory 获取可执行文件的root路径

func GetRootDirectory

func GetRootDirectory() (string, error)

GetRootDirectory 获取当前项目的根路径在哪里

func GetRootFilePath

func GetRootFilePath(filename string) (string, error)

GetRootFilePath 返回根路径下的文件路径

func GetSourceCodeRootDirectory

func GetSourceCodeRootDirectory() (string, error)

GetSourceCodeRootDirectory 如果是从源代码运行的,则按此方式寻找项目的根目录

func ReadRootFile

func ReadRootFile(filename string) ([]byte, error)

ReadRootFile 读取项目根目录下的文件

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL