Grokking Go
Vote

0% completed

​

"Hello, World!" in Go

Writing the "Hello, World!" Program in Go

Understanding the Structure of a Go Program

  1. Package Declaration: "package main"
  1. Importing a Package: import "fmt"
  1. The main Function: "func main() {}"
  1. 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:

Go
Go

. . . .

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:

  1. Package Declaration (package main)
  2. Importing Packages (import "fmt")
  3. Main Function (func main() {})
  4. 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 import statement 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 the Println() function.

3. The main Function: "func main() {}"

func main() { // Code inside this function runs when the program starts }
  • Every Go program must have a main function.
  • The main function 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.
  • Println means "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:

  1. The program starts execution from the main package.
  2. It imports the fmt package, which is needed for printing text.
  3. The main function runs, which is the entry point of the program.
  4. Inside the main function, 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%


Vote for new content

On This Page

Writing the "Hello, World!" Program in Go

Understanding the Structure of a Go Program

  1. Package Declaration: "package main"
  1. Importing a Package: import "fmt"
  1. The main Function: "func main() {}"
  1. Printing Output: "fmt.Println("Hello, World!")"

Complete Explanation of the Code Flow