0% completed
"Hello, World!" in Go
On This Page
Writing the "Hello, World!" Program in Go
Understanding the Structure of a Go Program
- Package Declaration: "package main"
- Importing a Package: import "fmt"
- The main Function: "func main() {}"
- Printing Output: "fmt.Println("Hello, World!")"
Complete Explanation of the Code Flow
In this lesson, we will write and understand our first Go program: "Hello, World!". This is the simplest program that every beginner writes in a new programming language to understand its basic syntax and structure. We will go through each part of the code step by step.
Writing the "Hello, World!" Program in Go
Below is a simple Go program that prints "Hello, World!" to the screen:
Let's break this code down and understand what each part does.
Understanding the Structure of a Go Program
A Go program consists of several key components, which are:
- Package Declaration (
package main) - Importing Packages (
import "fmt") - Main Function (
func main() {}) - Printing Output (
fmt.Println("Hello, World!"))
Let's go through each of these one by one.
1. Package Declaration: "package main"
package main
- Every Go program starts with a package declaration.
- A package is a way to group related Go files together.
- The main package is special because it tells the Go compiler that this program is an executable program (rather than a reusable library).
- If we don’t specify
package main, the program won’t compile as an executable.
2. Importing a Package: import "fmt"
import "fmt"
- The
importstatement allows us to use functions from other Go packages. "fmt"is a built-in package in Go that provides functions for formatted I/O (Input/Output).- We need
"fmt"to print text to the screen using thePrintln()function.
3. The main Function: "func main() {}"
func main() { // Code inside this function runs when the program starts }
- Every Go program must have a
mainfunction. - The
mainfunction is the starting point of execution. - When we run the program, Go automatically looks for
func main()and starts running the code inside it.
4. Printing Output: "fmt.Println("Hello, World!")"
fmt.Println("Hello, World!")
fmt.Println()is a function that prints text to the console."Hello, World!"is the message being printed.Printlnmeans "Print Line", which means it prints the text and moves to a new line after printing.
Complete Explanation of the Code Flow
Now, let’s look at the full flow of the program:
- The program starts execution from the main package.
- It imports the
fmtpackage, which is needed for printing text. - The
mainfunction runs, which is the entry point of the program. - Inside the
mainfunction,fmt.Println("Hello, World!")executes and prints"Hello, World!"to the screen.
In the next lesson, we will learn about comments in Go.
Reading Progress
0%
On This Page
Writing the "Hello, World!" Program in Go
Understanding the Structure of a Go Program
- Package Declaration: "package main"
- Importing a Package: import "fmt"
- The main Function: "func main() {}"
- Printing Output: "fmt.Println("Hello, World!")"
Complete Explanation of the Code Flow