S
S
s602021-07-12 11:54:10
go
s60, 2021-07-12 11:54:10

Running a go program in VS Code without debugging gives "go: go.mod file not found in current directory". How to fix?

To learn Golang, I took VS Code as an IDE .
Added plugin for Go. Specified $GOPATH$ (a folder with projects), in which there are three folders {src, pkg, bin). Created a new *.go file in the src folder with the text of the program "Hello world!".

program text
package main

import "fmt"

func main() {
  fmt.Println("hello world")
}



Runs from the command line without whims, even through run , even through build . But Run without debugging (without debugging - so that Delve does not get out of problems ) from VS Code always gives some problems.
One of them is the
" Failed to continue: Check the debug console for details. [Open launch.json] [Cancel] " popup.
HZ what needs to be added / edited in this launch. json.
And in DEBUG CONSOLE " go: go.mod file not found in current directory or any parent directory; see 'go help modules '."
But there are no third-party modules in the program. Only "import "fmt"" for PrintLn().
I googled that I need to run "go mod init" - I executed it in the console in the project folder "go mod init test.go" - a go.mod file was created in the project folder with the content
"module test
go 1.16"

contents of launch. json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch test function",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "program": "${workspaceFolder}",
            "args": [
                "-test.run",
                "MyTestFunction"
            ]
        },
        {
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceFolder}"
        },
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${file}"
        }
    ]
}



But when you run Run without debugging , the message about "go.mod not found" still appears.
Contents of the src folder:
  • go.mod (created via go mod init test.go)
  • test.exe (created via go build test.go)
  • test.go (Hello World program text)


In books about Go, work with code is done on the command line and this is not mentioned, since it refers to the settings of a particular IDE.

The question is: what tweaks do I need to make with VS Code to "smooth" Golang and in particular avoid this " go.mod issue "? Who doesn't find the go.mod file and why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
s60, 2021-07-12
@s60

Googled the solution Added GO111MODULE=auto
to Windows environment variables , restarted PC and everything worked in VS Code

N
NeoCode, 2021-09-21
@NeoCode

You need to create a go.mod file that specifies the name of the project (based on this name, the name of the executable file is generated). The file is located in the source folder. The content of the file is very simple:

module go1

go 1.17

here go1 is the name of the project (module).
The file can be generated on the command line with the command go mod init <module_name>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question