16

I recently started using Go. I installed go's extension on vscode and I can use some commands like go run and go build but when I run go test I get the following error:

go: cannot find main module; see 'go help modules'

although I have a *_test.go file.

When I run go help modules, I get the following output:

Modules are how Go manages dependencies.

A module is a collection of packages that are released, versioned, and
distributed together. Modules may be downloaded directly from version control
repositories or from module proxy servers.

For a series of tutorials on modules, see
https://golang.org/doc/tutorial/create-module.

For a detailed reference on modules, see https://golang.org/ref/mod.

By default, the go command may download modules from https://proxy.golang.org.
It may authenticate modules using the checksum database at
https://sum.golang.org. Both services are operated by the Go team at Google.
The privacy policies for these services are available at
https://proxy.golang.org/privacy and https://sum.golang.org/privacy,
respectively.

The go command's download behavior may be configured using GOPROXY, GOSUMDB,
GOPRIVATE, and other environment variables. See 'go help environment'
and https://golang.org/ref/mod#private-module-privacy for more information.
blackgreen
  • 34,072
  • 23
  • 111
  • 129
AliReda-M
  • 329
  • 1
  • 3
  • 12
  • 2
    For further reference; `go mod init` is part of the first step, in the fist link you’ve shown, and covered in detail in the second link. – JimB Mar 08 '21 at 02:26

1 Answers1

35

You have to initialize the project before you can run test:

go mod init puppy

https://golang.org/cmd/go#hdr-Module_maintenance

Zombo
  • 1
  • 62
  • 391
  • 407