9. January 2017 13:25
If you are using Go extension for Visual Studio Code to develop Google App Engine backend code on a Windows machine, you may have encountered strange problems related to environment variables GOROOT and GOPATH, specially when you want to use 3rd party libraries and expect the extension to correctly highlight code errors.
After lots of experiments, I ended up with the following setup which seems to be working as desired:
- Install Google App Engine Go SDK
- Setup GOROOT environment variable in your Windows environment (through Advanced System Settings). It should point to something like "C:\Program Files\Google\go_appengine\goroot"
- Add C:\Program Files\Google\go_appengine\goroot\bin path to your PATH environment variable
- Create a batch file in your code folder to setup GOPATH and run Visual Studio Code:
- Assuming your code folder is located in C:\Users\YourName\Project\API, your batch file will be C:\Users\YourName\Project\API\api.cmd
- Content of the batch file will be:
- Install Visual Studio Code
- Do not run it as Administrator (it somehow conflicted with execution of the batch file we created above, if batch file is not executed as Administrator)
- Execute the batch file to setup the environment variable and run Visual Studio Code
- Install Go Extension Go extension
- You may have to close and reopen Visual Studio repeatedly to apply the changes
- Let extension install the libraries it needs
- Do not use settings.json file to define environment variables as extension suggests
Now, you can enjoy the code completion and other benefits of the Go Extension!
In case you want to use 3rd party Go libraries, you can open a command prompt in your project folder and install the library locally. For example if you run the following in your project folder:
goapp get github.com/satori/go.uuid
It will install the above library inside "src" and "pkg" subfolders and you can import it to your Go project.
PS: I tried to use the "vendor" feature, but not sure why it did not work with App Engine version of Go.