main()
Function in GoEvery Go program must have a main
package, and inside it, a main()
function. This is where your program starts executingβjust like the engine that starts a car π.
main()
function, your Go application won't run.
package main import "fmt" func main() { fmt.Println("Hello, World!") }
Explanation:
package main
: tells Go this is a standalone program.import "fmt"
: imports the fmt package to use print functions.func main()
: the entry point of the program.Hello, World!
main()
from the main
package, Go will throw an error:
go: cannot find main module; see 'go help modules'
No. You can have only one main()
in your main
package. If you have more than one, the program won't compile.
package main import "fmt" func main() { var name string = "Technorank" fmt.Println("Welcome to Go, " + name + "!") }
Output:
Welcome to Go, Technorank!
You can test the main()
function live on:
main()
is the starting point of every Go programmain()
function is allowed in the main
packageHelp others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!