goversion_pkgpath_gte_go111.go 520 B

123456789101112131415161718192021222324
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. // +build go1.11
  4. package main
  5. import (
  6. "fmt"
  7. "golang.org/x/tools/go/packages"
  8. )
  9. func pkgPath(dir string) (string, error) {
  10. pkgs, err := packages.Load(&packages.Config{Dir: dir}, ".")
  11. if err != nil {
  12. return "", err
  13. }
  14. if len(pkgs) != 1 {
  15. return "", fmt.Errorf("Could not read package (%d package found)", len(pkgs))
  16. }
  17. pkg := pkgs[0]
  18. return pkg.PkgPath, nil
  19. }