Reputation: 353
Hi I am new to Go and currently use VS Code as IDE.
I am totally new to back-end development and I am trying to use Go for the job.
While I was teaching myself via Youtube, I faced a problem.
The problem is that the VS Code does not auto-import any package made by me.
I don't know why but I did get some clues about it.
My Guess
also my projects are located at C:\Users\John\Desktop\GoProjects while the gopath=C:\Users\John\go and the goroot=c:\go
Can anyone give me a solution to this?
Upvotes: 21
Views: 40660
Reputation: 498
You should have go.mod
file for autoimporting. Just run go mod init example.com/test-app
then go mod tidy
and autoimporting will work fine.
Upvotes: 1
Reputation: 331
Just check Go: Autocomplete Unimported Packages in File > Preferences > Settings It worked for me
Upvotes: 3
Reputation: 902
There is no issue in my GOPATH or GOROOT but auto import not working in my case & there is some squiggly alert in my code. I fix this by following these simple 3 steps.
I did the same thing. now everything works perfectly :).
Upvotes: 11
Reputation: 4644
In my case disabling gopls
worked and easily solved the issue.
File -> Preferences -> Settings -> Use Language Server (Type in search box and uncheck it)
Read the complete thread here for detailed information : https://github.com/microsoft/vscode-go/issues/2473
You can also import missing libraries using Command Palette (Cntrl + Shift + P) which is also very simple.
Open the Command Palette and run the command Go: Add Import to get a list of packages that can be imported to your Go file. Choose one and it will get added in the import block of your Go file.
Upvotes: 13
Reputation: 353
Solved. It's not a 100% perfect one though.
I don't know why Go is so inconvenient about this but here's my solution.
This part is important because this is where the path, your editor begins your relative path
Create three directories; bin, pkg, src
Ctrl + Shift + P => goinstall Install/Update tools
In the main.go file, import the package manually.
I hope this could help people facing the same problem! Happy coding!
Upvotes: 0