Usage of "go/import"

I’m trying to use “go/import” to find all the types in a package without much success.

When I use the example code from a similar StackOverflow answer it all works fine and the output makes sense.

package main

import (
    "fmt"
    "go/importer"
)

func main() {
    pkg, err := importer.Default().Import("time")
    if err != nil {
        fmt.Printf("error: %s\n", err.Error())
        return
    }
    for _, declName := range pkg.Scope().Names() {
        fmt.Println(declName)
    }
}

But when I change the package time to . or the import path of one of my packages, I get an error message that reads:

error: can't find import: /Users/mgilbir/Projects/go/src/bitbucket.org/mgilbir/importest

To be honest I’m at a loss of ideas of where to look next. What am I missing? How can I get this code working for the current package?

One of the problems with the go importer is it requires the package to be compiled first (this may be an oversimplification of the issue). I know that Alan Donovan and Robert Griesemer are working on improving the importer to be able to work with Go source.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.