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
?
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 functionmain
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With