How To fix gopls was not able to find modules

How to fix gopls was not able to find modules in your workspace. When outside of GOPATH, gopls needs to know which modules you are working on.

How To fix gopls was not able to find modules

Its very simple. let's start with how to get this error I'm assuming you already have installed go lang in your machine.

🚀Step 1:-

Now just go to desktop and create a folder called go lang and open that in VS Code.

🚀Step 2:-

Create a folder called cards inside there create main.go and paste the following code given below

package main
import (
    "fmt"
)
func main() {
    // var card string = "Ace of Spades"
    cards := []string{"Ace of Diamonds", newcard()}
    cards = append(cards, "Six of shades")

    for i, card := range cards {
        fmt.Println(i, card)
    }
}
func newcard() string {
    return "Five of Demonds"
}

🚀Step 3:-

in Golang folder create a file called cs.go and paste the code below

package main
import (
    "fmt"
)
func main() {
    cards := "Ace of shades"     
    fmt.Println(cards)
}

The Folder would look like this

🚀Step 4:-

Now open the terminal and in problem you see the error looks like this

🚀Step 5: -

Let's fix this. Now go back to the terminal and see your path. and type

cd cs

🚀Step 6: -

Now you are in cs directory. Run

go mod init cs

Save this file CTRL +S. See Error is gone.

Enjoy and give a red/pink heart if it helps you fix the issue. happy Coding.