modload

package
v1.1.18 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindGoMod

func FindGoMod(dir string) string

FindGoMod returns the nearest go.mod by walk dir and dir's parent and forefathers in order

func FindModuleName

func FindModuleName(modRoot string) (name string, err error)

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

func FindModuleRoot(dir string) (root string)

FindModuleRoot returns the nearest dir of go.mod by walk dir and dir's parent and forefathers in order

func ImportFile

func ImportFile(file string) (srcdir, importPath string, err error)

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

func ImportPackage(importPath string, dirs ...string) (srcdir string, modName string, err error)

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

func ModEnabled(dir string) bool

Types

This section is empty.

Jump to

Keyboard shortcuts

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