Introduction
Every programmer’s journey begins with “Hello World” - a simple program that displays a message. In Go, this classic first program demonstrates the basic structure of a Go application and how to produce output.The Code
Understanding the Structure
Let’s break down each part of this program:Package Declaration
package declaration. The main package is special - it defines a standalone executable program rather than a library.
Importing Packages
import statement brings in the fmt package, which contains functions for formatted I/O (similar to C’s printf and scanf). We need this to print output.
The Main Function
main function is the entry point of the program - execution starts here. We call fmt.Println to print the string “hello world” followed by a newline.
The
main function takes no arguments and returns no values. It’s automatically called when you run your program.Running the Program
To run this program, save it ashello-world.go and execute:
Key Takeaways
- Every Go program starts with a
packagedeclaration - The
mainpackage andmain()function create an executable program - Use
importto include packages you need fmt.Printlnis used for printing output with a newline- Go programs can be run directly with
go runor compiled withgo build