GO Tutorial



MAIN() FUNCTION IN GO


πŸš€ Understanding the main() Function in Go

Every 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 πŸš—.

πŸ”‘ Note: Without the main() function, your Go application won't run.

πŸ’» 1. Basic Structure of main()

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.

πŸ“₯ 2. Output of main()

Hello, World!
  
⚠️ Warning: If you remove main() from the main package, Go will throw an error:

go: cannot find main module; see 'go help modules'

🧠 3. Can You Have Multiple main() Functions?

No. You can have only one main() in your main package. If you have more than one, the program won't compile.

πŸ§ͺ 4. Let's Add Some Logic Inside main()

package main

import "fmt"

func main() {
    var name string = "Technorank"
    fmt.Println("Welcome to Go, " + name + "!")
}
  

Output:

Welcome to Go, Technorank!
  

πŸ’‘ Try It Yourself!

You can test the main() function live on:

πŸ”— Go Playground
βœ… Summary:
  • main() is the starting point of every Go program
  • Only one main() function is allowed in the main package
  • Code execution begins inside this function

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review