Why does VS Code not auto-import packages using Go?
Solution 1
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.
Solution 2
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.
- set GOPATH to my working directory, in this case C:/Users/John/GoProjects
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!
Solution 3
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.
- CTRL + SHIFT + P then type go tool
- Select install/Update Tools then select all checkbox
- Click ok & download all packages. Now close & reopen vs code it will remove all squiggly lines & auto-import packages
I did the same thing. now everything works perfectly :).
Johntopia
I use programming to solve problems. I am capable of Flutter, Javascript and Python. Right now I am trying to learn Swift. If you have any questions about me or just sth to discuss about email me at [email protected]
Updated on June 09, 2022Comments
-
Johntopia almost 2 years
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
- My editor does not recognize the location of the package
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?
-
super over 3 yearsvscode does all of this automatically for me. But then i use go modules for my project.
go mod init packagename
in the project directory and you're done.