package main import ( "go/build" "fmt" ) func main() { pkg, err := build.ImportDir("/path/to/package", 0) if err != nil { fmt.Println(err) return } fmt.Println(pkg.Name) }
package main import ( "go.build" "context" "fmt" ) func main() { ctx := context.Background() // Use the context for the build process pkg, err := build.ImportDirContext(ctx, "/path/to/package", 0) if err != nil { fmt.Println(err) return } fmt.Println(pkg.Name) }In this example, we use the Context type to cancel the build process if it takes too long. The ImportDirContext function is used to import the package from a directory using the provided context. Package Library: The go.build package is a standard library package.