error with "package main" flagging red in the main.go folder
Asked Answered
G

5

11

I have written a main.go program and declared the package at the beginning of the code but it flagging red as shown below: see the flag on the package main

When I hover on the package, it displays the following:

gopls requires a module at the root of your workspace.
You can work with multiple modules by opening each one as a workspace folder.
Improvements to this workflow will be coming soon, and you can learn more here:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.

I'd require help on how I can pass this error. Thanks a lot.

Giant answered 10/2, 2021 at 22:40 Comment(0)
A
0

I'm getting started with golang, so I'm not sure if this would help you.

This error was showing up here in VS Code, and to solve it I needed to clone an empty github repo into a new folder, to add the main.go file later.

I was able to add, commit and push changes. The lint error is now gone.

In my case, .git folder is located upwards, and not alongside main.go:

\my-app
  |
  |__ \backend
      |__ go.mod
      |__ main.go
  |__ \frontend
  |__ \.git

Within the backend folder the go.mod was generated with

go mod init github.com/<your-username>/my-app

It seems GitHub changed the default branch from master to main. If you use github, check if your in the correct branch, which may be main by default.

Ashelyashen answered 5/3, 2021 at 20:24 Comment(1)
Is using a github the only way to do this? I dont have a git repository and dont want one for what im doing.i have go.mod but im still seeing the errorBonita
M
2

You need to run the command go mod init from your command line at the root of the project. It looks like you ran it from go-microservice/prod-api, but it should be run at the same place where your .git lives. If prod-api is in fact the root of the git project, then you need to open that folder in VSCode instead of go-microservice.

Matthiew answered 11/2, 2021 at 3:55 Comment(0)
A
0

I'm getting started with golang, so I'm not sure if this would help you.

This error was showing up here in VS Code, and to solve it I needed to clone an empty github repo into a new folder, to add the main.go file later.

I was able to add, commit and push changes. The lint error is now gone.

In my case, .git folder is located upwards, and not alongside main.go:

\my-app
  |
  |__ \backend
      |__ go.mod
      |__ main.go
  |__ \frontend
  |__ \.git

Within the backend folder the go.mod was generated with

go mod init github.com/<your-username>/my-app

It seems GitHub changed the default branch from master to main. If you use github, check if your in the correct branch, which may be main by default.

Ashelyashen answered 5/3, 2021 at 20:24 Comment(1)
Is using a github the only way to do this? I dont have a git repository and dont want one for what im doing.i have go.mod but im still seeing the errorBonita
P
0

If you move hello.go to the root folder of GO-MICROSERVICE from prod-api, then you will not have an error.

Progressist answered 9/4, 2021 at 7:56 Comment(0)
T
0

You can disable this issue by adding "build.experimentalWorkspaceModule": true in gopls settings
This mentioned in the link appear in the error

Throw answered 23/8, 2021 at 23:17 Comment(0)
W
0

It is possible that the solution is very simple, like it was in my case. In my case it was as simple as the hierarchy of the folder i opened in VSCode.

enter image description here

If you look at my project hierarchy, I was getting the error when in VSCode I opened the folder called "start", which is the immediate folder where main.go was is. I was getting the highlighted red package error like you.

Then in VSCode, I selected "open folder", and opened the project one level above, folder "go_start_code". The error went away.

Wohlert answered 12/4, 2023 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.