Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFullImportPath ¶
GetFullImportPath resolves the full Go import path for any file or directory within a Go module. Returns the complete import path like "github.com/user/repo/pkg".
startAt can be either:
- A Go source file path (e.g., "/path/to/project/main.go")
- A directory path (e.g., "/path/to/project/pkg")
- An absolute or relative path
Returns an error if:
- No go.mod found in the directory tree
- go.mod is malformed or missing module directive
- Path resolution fails
Example (Directory) ¶
package main
import (
"fmt"
"log"
"github.com/cweill/gotests/internal/gomod"
)
func main() {
// Get import path for a package directory
importPath, err := gomod.GetFullImportPath(".")
if err != nil {
log.Fatal(err)
}
fmt.Println(importPath)
}
Output: github.com/cweill/gotests/internal/gomod
Example (File) ¶
package main
import (
"fmt"
"log"
"github.com/cweill/gotests/internal/gomod"
)
func main() {
// Get import path for a specific Go file
importPath, err := gomod.GetFullImportPath("gomod.go")
if err != nil {
log.Fatal(err)
}
fmt.Println(importPath)
}
Output: github.com/cweill/gotests/internal/gomod
Example (ModuleRoot) ¶
package main
import (
"fmt"
"log"
"github.com/cweill/gotests/internal/gomod"
)
func main() {
// Get import path for the module root directory
importPath, err := gomod.GetFullImportPath("../..")
if err != nil {
log.Fatal(err)
}
fmt.Println(importPath)
}
Output: github.com/cweill/gotests
Types ¶
This section is empty.