Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang func main() in packages other than main?

Tags:

go

In the case, for example, of helloworld, the name of the package is main and there is also a func main() statement. But I have also seen code which func main() which is in some other package. The code, however, seems like it might be used as a standalone program. So what does it mean to have a func main() with a package statement other than package main?

like image 512
Jeff Avatar asked Jun 05 '18 17:06

Jeff


Video Answer


1 Answers

The Go programming language is defined by its specification.


The Go Programming Language Specification

Program execution

A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.

func main() { … }

Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.


The function name main has no special significance outside package main.

like image 69
peterSO Avatar answered Oct 30 '22 07:10

peterSO