Fundamentals 4 min read

Write Your First Go Program: A Step‑by‑Step Hello World Tutorial

This tutorial walks you through creating, running, and understanding a simple Go "Hello World" program, covering environment setup, code compilation methods, and line‑by‑line explanations of the source code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Write Your First Go Program: A Step‑by‑Step Hello World Tutorial

First Go Program

This is the second tutorial in the Golang series; if you need an introduction to Go or installation instructions, refer to the previous article.

The best way to learn a programming language is by writing code, so let’s create the first Go program.

We recommend using Visual Studio Code with the Go extension (or GoLand) as the IDE, which provides autocomplete, code styling, and many other features.

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

Run Go Program

There are several ways to run the program:

Use go run helloworld.go at the command prompt; the console will display Hello World.

Use go install hello, then execute $GOPATH/bin/hello.

Use the Go Playground (online) to run the program quickly; a link to a pre‑created hello world example is provided.

Explain the Hello World Program

The following code is the program we just wrote:

package main //1
import "fmt" //2
func main() { //3
    fmt.Println("Hello World") //4
}

Explanation of each line:

package main – Every Go source file starts with a package declaration; main indicates an executable program.

import "fmt" – Imports the fmt package, which provides formatted I/O functions such as printing to standard output.

func main() – The entry point of the program; execution begins here.

fmt.Println("Hello World") – Calls Println from the fmt package to write the string Hello World to the console.

The source code can be downloaded from the linked GitHub repository.

Next, proceed to the third part of the Golang series to learn about variables.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Goprogramming basicsHello World
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.