Error: Package awesomeProject contains more than one main function Consider using File kind instead

question

There is a green arrow next to the code, and the edit configuration window will pop up automatically when you click Run, which makes it impossible to run

ERROR: Package awesomeProject contains multiple main functions Consider using filetype instead 

--------------------------------------------------------------------------------------------------------------------------------

package (package)

All code must be organized in packages

A package is essentially a "directory" with many go files in it. It can also be said that a package consists of multiple source files in the same directory

In the same package directory, the first line of all .go files needs to add the package definition, and the program marks the source code file according to this

package 包名

Note: The code package name and file directory can be inconsistent, but the package defined in the first line of each source file in the same directory must be consistent

main package

- The package where the program entry main function of go language is located

- Under the main package, if you want to refer to the code defined in other packages, you need to use the import keyword, that is, import the package

Note: The main package is the entry package of the program, other packages cannot be used

The first time you come into contact with the Go language, of course you have to start with Hello World

Create a Go file under the project, hello.go

package main

import "fmt"//导入一个系统包fmt用来输出的

func main() {
	fmt.Println("Hello,World!")//打印输出字符串
}

The package main on the first line defines the package name, package main represents a program that can be executed independently, and every Go application contains a package named main

func main() is the function that the program starts to execute. All executable Go programs must have a main function (usually the first function executed after startup)

Summary: The Go executable program can be decomposed into packages one by one. The main package must exist, and the main package must contain the main function. The execution of the program is essentially to run the main function of the main package. After the main function ends, the program ends up

---------------------------------------------------------------------------------------------------------------------------------

So why does the above error appear?

Because there must be only one main function in a main package, there cannot be two or more

Goland functions do not support overloading. A package cannot have two functions with one function name. If two function execution entries appear at the same time, the system will not be able to select the correct execution entry and report an error.

Because I tried two go source code files in the main package at the beginner stage, the above error occurred

Normally, the package where the main function is located should keep only one go source code file.

Solution

It's not impossible to have to run

Right-click on the source file to run it

other considerations

https://learnku.com/articles/65136

Guess you like

Origin blog.csdn.net/weixin_43819762/article/details/128621817