Home  >  Article  >  Backend Development  >  What does main mean in go language?

What does main mean in go language?

青灯夜游
青灯夜游Original
2022-12-09 18:40:506710browse

In the Go language, main means "main" and is the default entry function (main function) of the Go language program. It is a function without any parameters and return values; the main function is the entry point of the program. In other words, the execution of the program must start from the main function. There can only be one main function in the entire program. If there are multiple main functions, the program will not run normally. All our custom functions must be called directly or indirectly in the main function, otherwise they will not run (except the init function).

What does main mean in go language?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

In the Go language, main means "main" and is the default entry function (main function) of the Go language program.

The main function of Go language


#The Go language program has a special function, which is the main function. The main function is the function of the program. Entry, that is to say, the running of the program must start from the main function.

There can only be one main function in the entire program. If there are multiple main functions, the program will not run normally. All our custom functions must be called directly or indirectly in the main function, otherwise they will not run (except the init function).

Syntax

func main(){
   // do something
}

Description

The main function of Go language is a function without any parameters and return value. [Related recommendations: Go video tutorial, Programming teaching]

main function case


The custom function must be called by the main function

The custom function must be called by the main function in order to run

package main
import (
	"fmt"
)
func myFunc(){
	fmt.Println("Call myFunc")
}
func myFunc2(){
	fmt.Println("Call myFunc2")
}
func main() {
	//自定义函数必须被main函数调用,才能运行
	myFunc2()
}

After the program is run, the console output is as follows:

What does main mean in go language?

We defined two functions, one myFunc and one myFunc2, but we only called myFunc2 in the main function and did not call myFunc, so we see our The program only outputs the contents of myFunc2, not the contents of myFunc.

Therefore, all functions must be directly or indirectly called by the main function before they can run.

Go language main function

The running of the Go language program starts from the main function

package main
import (
	"fmt"
)
func myFunc(){
	fmt.Println("Call myFunc")
}
func main() {
    //Go语言程序的运行,是从 main 函数开始的
	fmt.Println("Main Start")
	myFunc()
	fmt.Println("Main End")
}

After the program runs, the console output is as follows:

What does main mean in go language?

We print a sentence at the beginning of the main function, then we call the customized function myFunc, and finally, when the main function ends, we print a sentence again.

We see that first the program prints the sentence at the beginning of the main function, then prints the output from the function we called, and finally prints the end of the main function again. It can be seen from this that our program starts from the main function and ends with the main function.

Go language main function summary


Go language program has a special function, which is the main function. The main function is the function of the program. Entry, that is to say, the running of the program must start from the main function. All our custom functions must be called directly or indirectly in the main function, otherwise they will not run (except the init function). Go language main function syntax:

func main(){
   // do something
}

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of What does main mean in go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn