Skip to content

Instantly share code, notes, and snippets.

@ysugimoto
Created August 18, 2018 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysugimoto/8d1f63f2eb1275bb9b39cb1cdd6cded5 to your computer and use it in GitHub Desktop.
Save ysugimoto/8d1f63f2eb1275bb9b39cb1cdd6cded5 to your computer and use it in GitHub Desktop.
list imports example
package main
import (
"fmt"
"github.com/satori/go.uuid"
"github.com/stretch/testify/assert"
)
func main() {
fmt.Println("vim-go")
}
package main
import (
"fmt"
"go/parser"
"go/token"
"net/url"
"strings"
)
func main() {
t := token.NewFileSet()
a, err := parser.ParseFile(t, "./example.go", nil, parser.ImportsOnly)
if err != nil {
panic(err)
}
for _, i := range a.Imports {
p := strings.Trim(i.Path.Value, `"`)
fmt.Print(p)
u, err := url.Parse(p)
if err != nil {
fmt.Println("error", err)
} else {
fmt.Println("succes", u.Host)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment