Go run error go run: cannot run non main package

I was born in Java. Recently I became interested in go language, so I will learn it with go in the future.

After the go environment is installed and configured, the vscode development tool is installed, and the first go program is written. It’s very simple, just a simple output statement, but it does

Go run: cannot run non main package. The code is as follows:

package test
import "fmt"
func main() {
  fmt.Println("cainiaobulan testing go")
}

The error message of go is very straightforward. The main method can only be placed in package main. Go run is to execute the command and a main must be used to call it. Install can be directly compiled into a package file or an EXE (if there is a main function)

Change the code and run OK

package main
import "fmt"
func main() {
  fmt.Println("cainiaobulan testing go")
}

Just take this mistake as the first step for me to learn go. In the future, I can cross the pit step by step. I hope that children’s shoes with the same interest can learn together and carry forward go. At the same time, I also want to seek guidance from the boss. I am ready to develop in the direction of go web.

Read More: