7

I have been trying to use the importer to parse the types defined in a particular package. However, the importer always return an error saying the package is not found. What mistake I am making?

package main

import (
    "fmt"
    "go/importer"
)

func main() {
    pkg, err := importer.Default().Import("github.com/onsi/ginkgo")
    if err != nil {
        panic(err)
    }
    fmt.Println(pkg)
}

I tried to read the go importer documentation, but it provides very limited information. I also tried to use the package I am importing here, but it does not help. However, if I import a go standard package, such as “time”, I can currently import the package. Why is that?

Yifan Sun
  • 772
  • 1
  • 10
  • 25
  • are you trying to dynamically import during runtime ... or as below question explains are you cool with just doing import before compile time ? – Scott Stensland May 22 '19 at 14:52
  • Just a clarification on what I need. I am detecting package information at runtime. I also mainly use go modules rather than GOPATH. – Yifan Sun May 22 '19 at 15:19
  • 1
    While I think there's supposed to be a way to do this with the existing `go/importer` package which internally calls out to the `go` tool to locate the module source, I think you want to look at [`golang.org/x/tools/go/packages`](https://godoc.org/golang.org/x/tools/go/packages) which is supposed to supersede the `go/importer` package. – JimB May 22 '19 at 18:03
  • Hi @YifanSun Can you guide me to use this package? I have tried but it returns can't find import, if I import directly the package by using import statement, I can use the struct in the package. – Mark Smith Jun 05 '23 at 15:32

1 Answers1

2

Go importer will not download the package for you. You can use dep or go modules to handle your dependencies, but an easy fix would be downloading the package directly to your gopath using go get:

go get -u github.com/onsi/ginkgo

After that, go importer will work and your code output should be:

package ginkgo ("github.com/onsi/ginkgo")

[EDIT] Using Go Modules:

There's a bunch of tutorials about that, but the quick and dirty way is, on your package directory:

$ GO111MODULE=on go mod init
$ GO111MODULE=on go mod tidy

That will check your project and download all packages. To install a specific package on your go.mod, you can use:

$ go install github.com/onsi/ginkgo
  • I tried this with GOPATH and it works. So my question is how to get the same thing working with Go Modules? – Yifan Sun May 22 '19 at 15:15
  • There's a bunch of tutorials about that, but the quick and dirty way is, on your package directory: $ GO111MODULE=on go mod init $ GO111MODULE=on go mod tidy That will check your project and download all packages. To install a specific package on your `go.mod`, you can use: $ go install github.com/onsi/ginkgo – Márcio Rocha Zacarias May 22 '19 at 15:24
  • I know how to set up a go module. However, the problem is that if the package I am importing is in GOPATH, i can import it. But if the package is not in the GOPATH, the `importer` cannot find it. But since I am using GOPATH at all, I cannot get all the packages in the GOPATH. – Yifan Sun May 22 '19 at 15:48
  • Hmm, got it... It's a tricky scenario... I didn't tested yet, but I presume that `go mod tidy` will cleanup ginkgo, as it's not directly imported by `import` on your code. My last guess would be maybe vendoring your package with `$ go mod vendor`? Not sure if, similar to `tidy`, if it will not override any changes on your `go.mod` config and eliminate not "imported" packages – Márcio Rocha Zacarias May 22 '19 at 17:52
  • 1
    Yes, `go mod tidy` will remove the package which isn't imported. The old `Import` interface does not work with vendor either: `CAUTION: This interface does not support the import of locally vendored packages.` – JimB May 22 '19 at 18:05