| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
runpath package provides func to get the execution location of Go code, including the absolute path of the source file.
Install the package with:
go get github.com/yylego/runpathrunpath is a runtime path utils package that obtains the absolute path of Go code execution locations.
Core Capabilities:
runpath Advantages:
Unlike the built-in filepath.Abs(".") which doesn't always provide the expected result in certain situations, runpath uses Go's runtime package to provide precise location tracking, making it valuable when the exact execution path is needed.
Path Operations:
DIR Operations:
Test Utilities (runtestpath):
This example shows how to get the current source file path and parent DIR information.
package main
import (
"fmt"
"github.com/yylego/runpath"
)
func main() {
// Get current source file path
currentPath := runpath.Path()
fmt.Println("Current source path:")
fmt.Println(currentPath)
// Get current source file name
currentName := runpath.Name()
fmt.Println("Current source name:")
fmt.Println(currentName)
// Get parent DIR path
parentPath := runpath.PARENT.Path()
fmt.Println("Parent DIR path:")
fmt.Println(parentPath)
// Get parent DIR name
parentName := runpath.PARENT.Name()
fmt.Println("Parent DIR name:")
fmt.Println(parentName)
}⬆️ Source: Source
This example shows how to navigate DIR structures and build paths.
package main
import (
"fmt"
"github.com/yylego/runpath"
)
func main() {
// Join paths with parent DIR
configPath := runpath.PARENT.Join("config.json")
fmt.Println("Config path:")
fmt.Println(configPath)
dataPath := runpath.DIR.Join("data", "example.txt")
fmt.Println("Data path:")
fmt.Println(dataPath)
// Navigate up DIR structure
upOne := runpath.PARENT.Up(1)
fmt.Println("Up 1 DIR:")
fmt.Println(upOne)
upTwo := runpath.PARENT.Up(2)
fmt.Println("Up 2 DIRs:")
fmt.Println(upTwo)
// Navigate up and join with path
moduleConfig := runpath.PARENT.UpTo(1, "config.json")
fmt.Println("Module config:")
fmt.Println(moduleConfig)
projectConfig := runpath.PARENT.UpTo(2, "config", "settings.json")
fmt.Println("Project config:")
fmt.Println(projectConfig)
}⬆️ Source: Source
This example demonstrates changing file extensions and working with config files.
package main
import (
"fmt"
"github.com/yylego/runpath"
)
func main() {
// Get current file path with different extension
jsonPath := runpath.GetPathChangeExtension(".json")
fmt.Println("JSON config path:")
fmt.Println(jsonPath)
// Using compact alias GetRex
yamlPath := runpath.GetRex(".yaml")
fmt.Println("YAML config path:")
fmt.Println(yamlPath)
// Get path without extension
basePath := runpath.GetNox()
fmt.Println("Base path (no extension):")
fmt.Println(basePath)
// Demonstrate use case: reading config file with same name
configPath := runpath.GetRex(".config.json")
fmt.Println("Config file path:")
fmt.Println(configPath)
}⬆️ Source: Source
You can use runpath to build paths to configuration files based on the execution location of the code. This is valuable in tests where different configurations are loaded depending on where the test is being executed.
path := runpath.DIR.Join("config.json")This constructs the path to config.json relative to the execution DIR abs-path.
You can also use PARENT with the same approach:
path := runpath.PARENT.Join("config.json")If you need to navigate up multiple DIR levels from the execution location, use:
path := runpath.PARENT.UpTo(3, "config.json")This will return the path to config.json located three levels up from the execution DIR abs-path.
When running tests, you might need to generate code and reference the source file path. Here's how you can locate the test file's source path:
func TestSrcPath(t *testing.T) {
path := runpath.SrcPath(t)
t.Log(path)
require.True(t, strings.HasSuffix(path, "runpath/runtestpath/utils_runtestpath.go"))
}This approach helps when generating code that needs to be placed alongside the source files based on the execution location.
You can also change the file extension depending on the test context (e.g., from .go to .json):
func TestSrcPathChangeExtension(t *testing.T) {
path := runpath.SrcPathChangeExtension(t, ".json")
t.Log(path)
require.True(t, strings.HasSuffix(path, "runpath/runtestpath/utils_runtestpath.json"))
}
func TestSrcRex(t *testing.T) {
path := SrcRex(t, ".json")
t.Log(path)
require.True(t, strings.HasSuffix(path, "runpath/runtestpath/runtestpath.json"))
}This allows you to load different types of files (e.g., configuration files) based on the execution location and test requirements.
This package is valuable in test files, where you need to reference source code paths and configuration files based on the execution location of the test file.
MIT License - see LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
New code contributions, follow this process:
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
Have Fun Coding with this package! 🎉🎉🎉
Give me stars! Thank you!!!
| Back | FazBrowse Home | New Git URL |