Setting up VSCode with Golang

Hello all. I’m hoping someone can help me figure out what I’m doing wrong here.

I’m working under Windows with VSCode and learning Go. I’m getting to some of the more complex concepts in Go and I’m trying to figure out why VSCode is ‘yelling’ at me.

When I work in VSCode, I’m getting a red underline under ‘package XXXX’ for whatever project I’m working on as well as a red underline under the the names of structures that are in other files. When I hover over the errors, I get “gopls was not able to find modules in your workspace” for the ‘package XXXX’ lines and I get an ‘undeclared name’ for the names of the structs.

For example, in the ‘election-day’ project, I get error indicators under ‘package electionday’ as well as when referencing the structure ‘ElectionResult’ that is in the separate file election_result.go.

Now, when I run my test, my test passes, but I’d really like to get the errors out of VSCode as well as understand what I did wrong to cause this to happen in the first place.

I’m doing all of my learning of Exercism on a separate virtual machine and can easily do any reinstallation/rebuilding to it that I’d need to, so anything anyone suggests, I’m open to trying. And if anyone wants to see any additional information/has questions, let me know. I’m happy to share.

Thanks in advance for any help anyone can provide!

Charles

1 Like

My suggestion for the best experience with Go in VSCode would be:

  • Make sure you are on the lastest version of Go

  • Make sure you have the Go extension installed, and when the Go extension asks you to install or update gopls, do so.

  • Open just the exercise folder in VSCode. You can do this by going to File > Open Folder... and then selecting the exercise folder. gopls and the Go extension have an easier time finding the module if it’s literally the only folder you have open, at least on that window of VSCode. If you have VSCode open on a folder that doesn’t have the go.mod of the exercise on its root or if you have a VSCode workspace that has multiple folders open (i.e a “multi-root” workspace), gopls might have an hard time making sense of it all.

    Here’s a sequence of commands that works really well for me:

    # Download the exercise locally if you haven't already.
    # Be sure to only run this if you don't have any local changes 
    # to the exercise you want to keep.
    $ exercism download --exercise=election-day --track=go --force
    
    Downloaded to
    /Users/andre/git/exercism/exercism/go/election-day
    
    # Open a new VSCode window in the directory of the exercise
    $ code /Users/andre/git/exercism/exercism/go/election-day
    

If after doing this you are still getting errors, let us know what errors exactly you are getting so we can provide a better help.

1 Like

Thanks, andrerfcsantos, for the start of my solution. So, thus far, your answer has helped a bit in letting me move forward.

What I WAS doing was working in VSCode from the ‘exercism’ folder and going into each directory as I got the new project to work on. That’s what was causing the red lines. With your suggestion, I’m not getting red lines anymore, but I am still getting VSCode complaining to me and getting yellow lines instead.

Like before, when I run my tests, everything works correctly, but I am still getting the yellow warning lines on, in this case the Go project of ‘elons-toys’, any declarations to the ‘Car’ struct. The specific warning I am getting is this: undeclared name: Car (compile) go-staticcheck

I’m working on a Windows 2016 virtual machine (in trial mode in case that matters), running the latest versions of VSCode as well as the Go modules. I’ll look into the ‘go-staticcheck’ module to see if that’s where my problem is.

Thank you,
Charles

Hey there, andrerfcsantos -

Yeah, I know, I should have waited before going ahead and sending my last reply. LOL!! Anyway, I’ve now found the other part of the solution.

Going through and getting things figured out while I was working in the ‘exercism’ directory and not the project’s directory, I made some changes to the Go plugin within VSCode and I just reverted one of those changes and this fixed it for me.

The issue was with ‘Go: Lint On Save’. One of the articles I’d read mentioned that I should set that to ‘file’. Turns out that I wanted this set on ‘package’ and it wasn’t. Changing the value to ‘package’ has taken care of the problem for me.

Thanks to you - and also to W8CYE for your generous offer - for helping me figure this all out. I greatly appreciate it!!

I’ve got a Meetup that I’m presenting at tomorrow regarding my experiences with learning with Exercism so far, so the timing to get this all figured out great!! Thanks again!

Charles

Happy to help :slight_smile: Glad you managed to solve the issue.

One more thing that is worth mentioning, is that if you really want to have a multi-module workspace (each exercise comes with its own go.mod file, so each exercise is a module), you can try to setup a multi-module workspace which requires you to have a go.work file telling the location of the modules. Last time I checked, gopls will actually recognize this file and some of the errors caused by being in a workspace with several modules will also disappear.