 Documentation
      ¶
      Documentation
      ¶
    
    
  
    
  
    Index ¶
- func FindGoMod(dir string) string
- func FindModuleName(modRoot string) (name string, err error)
- func FindModuleRoot(dir string) (root string)
- func ImportFile(file string) (srcdir, importPath string, err error)
- func ImportPackage(importPath string, dirs ...string) (srcdir string, modName string, err error)
- func ModEnabled(dir string) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindGoMod ¶
FindGoMod returns the nearest go.mod by walk dir and dir's parent and forefathers in order
func FindModuleName ¶
FindModuleName Extract module name from {modRoot}/go.mod
Example ¶
package main
import (
	"fmt"
	"os"
	"github.com/searKing/golang/go/go/cmd/go/modload"
)
func main() {
	cwd, err := os.Getwd()
	if err != nil {
		panic(fmt.Errorf("getwd: %w", err))
	}
	importPath, err := modload.FindModuleName(modload.FindModuleRoot(cwd))
	if err != nil {
		panic(fmt.Errorf("find mod name: %w", err))
	}
	fmt.Print(importPath)
}
Output: github.com/searKing/golang/go
func FindModuleRoot ¶
FindModuleRoot returns the nearest dir of go.mod by walk dir and dir's parent and forefathers in order
func ImportFile ¶
ImportFile finds the package containing importPath, and returns its source directory (an element of $GOPATH) and its import path relative to it.
Example ¶
package main
import (
	"fmt"
	"os"
	"github.com/searKing/golang/go/go/cmd/go/modload"
)
func main() {
	cwd, err := os.Getwd()
	if err != nil {
		panic(fmt.Errorf("getwd: %w", err))
	}
	//cwd = "/Users/chenhaixin/workspace/go/src/github.com/searKing/golang/go/go/cmd/go/modload/init.go"
	srcDir, importPath, err := modload.ImportFile(cwd)
	if err != nil {
		panic(fmt.Errorf("find import path: %w", err))
	}
	_ = srcDir
	fmt.Println(importPath)
}
Output: github.com/searKing/golang/go/go/cmd/go/modload
func ImportPackage ¶
ImportPackage finds the package and returns its source directory (an element of {dir}'s parent and forefathers in order).
Example ¶
package main
import (
	"fmt"
	"os"
	"github.com/searKing/golang/go/go/cmd/go/modload"
)
func main() {
	cwd, err := os.Getwd()
	if err != nil {
		panic(fmt.Errorf("getwd: %w", err))
	}
	//cwd = "/Users/chenhaixin/workspace/go/src/github.com/searKing/golang/go/go/cmd/go/modload/init.go"
	srcDir, modname, err := modload.ImportPackage("github.com/searKing/golang/go/go/cmd/go/modload", cwd)
	if err != nil {
		panic(fmt.Errorf("find import path: %w", err))
	}
	_ = srcDir
	fmt.Println(modname)
}
Output: github.com/searKing/golang/go
func ModEnabled ¶
Types ¶
This section is empty.
 Click to show internal directories. 
   Click to hide internal directories.