GoClipse / goclipse

Eclipse IDE for the Go programming language:
http://goclipse.github.io/
Eclipse Public License 1.0
840 stars 286 forks source link

Automatic import constuction on save #66

Open baconalot opened 10 years ago

baconalot commented 10 years ago

I saw some guys that were working with go in vi, that had a nifty feature that constructed the right imports on save. So when they put in the first fmt.Println("") it would automatically add "fmt" to the imports, and when a import was not needed anymore it would be removed. Cant remember what script did that, but would be nice to have.

andrewreds commented 10 years ago

There is a go package called goimports (https://godoc.org/code.google.com/p/go.tools/cmd/goimports) that does what you are wanting.

It basically replaces the normal gofmt command, with one that does import magic.

To use in goclipse:

1) install goimports: go get code.google.com/p/go.tools/cmd/goimports

2) in eclipse goto: Preferences -> Go -> Go Formatter and set it to be the path of the goimports bin

This will fix all imports when you format. If you combine this with #67, then imports will be automatically fixed on save.

baconalot commented 10 years ago

Brilliant, works like a charm. Maybe an idea to update the doc to mention this?

osechet commented 8 years ago

The new command to get goimports is: go get golang.org/x/tools/cmd/goimports