Below is the Hello world program in Golang:
package main
import "fmt" // Importing fmt package to use its functions
func main() {
fmt.Println("Hello World") // Using fmt.Println to print to the console
}
This program has only 4 statements for printing “Hello World” on the screen. Let us discuss each statement:
1. package main
package main
In Go, every source file starts with a package declaration, which defines the package the file belongs to. The Go language organizes code into packages, which are essentially containers for related Go files.
package main
specifically tells the Go compiler that this package is meant to be the entry point for the program. It indicates that the code in this file will be compiled into an executable program. Additionally, a Go program must contain a function called main()
within the main
package, which acts as the starting point of execution. If your program does not have package main
, it won't be compiled into an executable.
2. import "fmt"
The import
keyword is used to include packages that your program will use. In Go, standard library packages and third-party packages are imported when you need to access their functionality.
import "fmt"
imports the fmt (format) package, which is part of Go's standard library. The fmt
package provides I/O-related functions, like printing to the console and reading input.
Some commonly used functions in the fmt
package include:
fmt.Println()
: Prints output to the console with a newline at the end.fmt.Printf()
: Formats and prints output according to a format specifier.fmt.Scan()
: Reads input from the user.
3. func main()
func main()
is a function declaration that defines the entry point of a Go program. It is a special function because, when you run your program, the Go runtime starts executing code from the main()
function.
func
: This is a keyword in Go used to declare a function.main
: The name of the function.main()
is a special function in Go because it's the starting point for execution in any Go program.()
: Parentheses that follow the function name. In this case,main
doesn't take any parameters, so they are empty.
Why is func main()
important?
- Every executable Go program must have a
main()
function defined inside themain
package (package main
). - When you run the Go program, the
main()
function is where the execution begins. Without it, Go doesn't know where to start the program.
func main()
is mandatory for any Go program that you want to compile into an executable file.- You can think of
main()
as the function where the entire program begins executing. - Only one
main()
function is allowed per Go package. If you try to define more than onemain()
function, it will result in a compilation error.
Everything inside the main()
function is executed sequentially when the program starts. You can put any logic or function calls inside it. Once the main()
function finishes, the program terminates.
For example:
import "fmt"func main() {
fmt.Println("Starting the program...")
// You can call other functions from main
greet("Alice")
add(3, 5) fmt.Println("End of the program")
}// Custom function to greet someone
func greet(name string) {
fmt.Println("Hello,", name)
}// Custom function to add two numbers
func add(a int, b int) {
fmt.Println("Sum is:", a+b)
}
When this program runs, it will execute the main()
function first, which will print messages, call greet()
, and add()
. After main()
completes, the program ends.
In summary, func main()
is the heart of any Go program that defines what happens when the program starts running.