confusion about an import (with slightly strange suffix) being auto-deleted on save
I went to https://pkg.go.dev/rsc.io/quote/v4 and noticed that the import was rsc.io/quote/v4, so I put the following in VSCode saved it (an autoformat ran, but it was fine), then did "go mod tidy" and "go run ." and it worked fine.
However, when I tried the same thing in GoLand, I was unable to save without the autoformat deleting the "rsc.io/quote/v4" statement, probably because I'm using "quote.Glass()" instead of "v4.Glass()" in my actual call.
Am I doing something wrong here? Am I not supposed to include the "/v4" part in the import statement? But if I omit it, I end up with v1.5.2 of the quote package, which is much older. Besides, having "/v4" works fine in VSCode, and the formatter doesn't nuke my import statement. Is there some other way in GoLand that I'm supposed to be using instead of this?
package main
import (
"fmt"
"rsc.io/quote/v4"
)
func main() {
fmt.Println(quote.Glass())
}
Please sign in to leave a comment.
Hi,
What version of GoLand are you using (Help | Find Action | About)?
Will it work as expected if you specify go.mod file content first, synchronize dependencies from there and then use a code snippet?
I'm using GoLand 2022.3.4
I'm not sure what you mean by "synchronize dependencies", but I tried manually putting the "require ..." line, and then the autoformat-on-save no longer deleted. But I still had to do a "go mod tidy" for it to get the quote package's dependencies.
But the point is, on VSCode, the autoformat never deletes the import; I'm not required to go to go.mod first and manually insert a "require..." line.
Is rsc.io/quote/v4 import display in go.mod file as a red or green (downloaded) dependency?
sorry, I can't answer that question because I'm red-green colorblind XD
No worries, are there any actions available if you hover over rsc.io/quote/v4 dependency in go.mod file and press Alt(Option)+Enter?
so if I Alt + Enter after I've done "go mod tidy" and everything works, I see a small popup menu that says "go mod why -m rsc.io/quote/v4". I'm not sure what that does.
If I start a new project, then type the code with the import, deliberate *not* save, then manually enter the require statement in go.mod, and then do Alt + Enter, I get a slightly larger popup menu that has the "go mod why ..." thing mentioned above, but also an option for "go mod tidy". However, I selected "go mod tidy" and it *deleted* the require line, and also *deleted* the import statement in my code!
Definitely lots of jank here; I'm not even sure what the intended flow for import statements that don't follow the "ending with package name" convention is, though apparently VSCode handles it correctly.
EDIT: to clarify, the difference between the first and second paragraphs above is that in the first paragraph, I did "go mod tidy" by right-clicking go.mod file after manually putting in the require line. In second paragraph, I used the menu option from Alt+Enter while cursor is on the require line.
I got a vague impression that the reason the second paragraph failed was that something in the background was too to catch up, since when I got that failure, I did a bunch of undos, and then somehow like 3 seconds later the correct import statements appeared *back* in my code, and then the whole thing compiled and worked.
Still extremely confusing; I got the impression that all these auto-format and auto-linters (or whatever) were just pulling the rug out from under me
Thanks for the follow-up.
Could you please check that Go modules integration enabled in Settings | Go | Go Modules?
After that, you should have Sync Dependencies of <...> action in go.mod file or on unresolved import statements (please also make sure that this import is present in any Go file).