LibraryYour First Go Program: "Hello, World!"

Your First Go Program: "Hello, World!"

Learn about Your First Go Program: "Hello, World!" as part of Go Programming for Backend Systems

Your First Go Program: "Hello, World!"

Embarking on your journey with Go (Golang) begins with a fundamental tradition: writing a "Hello, World!" program. This simple program serves as your initial interaction with the Go compiler and runtime, demonstrating the basic structure of a Go application.

Understanding the "Hello, World!" Structure

A typical "Hello, World!" program in Go involves a few key components: a package declaration, an import statement, and a main function. Let's break down each part.

Every Go program starts with a `package main` declaration.

The package main declaration tells the Go compiler that this package should be compiled into an executable program, rather than a shared library.

In Go, code is organized into packages. The package keyword at the beginning of a file declares which package the file belongs to. For executable programs, this package must be main. The main package is special because it tells the Go toolchain that this package should be compiled into an executable binary. Without package main, your code would be treated as a library that other programs could import.

The `import "fmt"` statement brings in the formatting package.

The fmt package provides functions for formatted I/O (input/output), such as printing text to the console.

To use functionality from other packages, you need to import them. The fmt package is part of Go's standard library and is essential for tasks like printing output. The import keyword is followed by the name of the package in double quotes. When you need to use functions like Println from the fmt package, you must import it first.

The `func main()` is the entry point of your program.

The main function is where the execution of your Go program begins. It's the first function that runs when you execute the compiled binary.

Every executable Go program must have a main function. This function is the starting point of your program's execution. When you run your Go program, the Go runtime looks for and calls the main function within the main package. The func keyword declares a function, main is the function name, and () indicates that it takes no arguments. The curly braces {} enclose the body of the function, containing the code that will be executed.

Inside the

code
main
function, we use
code
fmt.Println("Hello, World!")
to display the message.
code
Println
is a function from the
code
fmt
package that prints its arguments to the standard output, followed by a newline character.

Here's the complete "Hello, World!" program in Go:

package main

import "fmt"

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

This code demonstrates the fundamental structure: package declaration, import statement, and the main function containing the print statement. The fmt.Println function is a key tool for outputting information.

📚

Text-based content

Library pages focus on text content

Running Your First Go Program

To run this program, you'll need to have Go installed. Save the code above in a file named

code
main.go
. Then, open your terminal or command prompt, navigate to the directory where you saved the file, and execute the following command:

code
go run main.go

This command compiles and runs your Go program. You should see the output:

code
Hello, World!

The go run command is a convenient way to execute Go source files without explicitly building an executable first. It's perfect for quick testing and learning.

What is the purpose of package main in a Go program?

It declares that the package should be compiled into an executable program.

Which standard library package is commonly used for printing output in Go?

The fmt package.

What is the entry point for execution in any Go executable program?

The main function.

Learning Resources

A Tour of Go: Hello, World(tutorial)

The official Go tour provides an interactive introduction to the language, starting with the classic 'Hello, World!' example.

Go by Example: Hello World(tutorial)

This site offers practical, runnable examples of Go programming concepts, including a clear explanation of 'Hello, World!'.

The Go Programming Language Specification - Packages(documentation)

Dive into the official Go language specification to understand the fundamental concepts of packages and their role in Go programs.

Go Packages and Imports Explained(blog)

A blog post detailing how packages work in Go, including importing and using them, with practical examples.

Golang Tutorial for Beginners: Your First Program(video)

A beginner-friendly video tutorial that walks through writing and running your very first Go program.

Go Programming Language - Official Website(documentation)

The official home of the Go programming language, offering documentation, downloads, and community resources.

fmt Package - Go Documentation(documentation)

Explore the official documentation for the `fmt` package, which is crucial for input and output operations in Go.

How to Write Go Code(documentation)

An essential guide from the Go team on setting up your Go workspace and writing your first Go programs.

What is Go? (Golang)(wikipedia)

A comprehensive overview of the Go programming language, its history, features, and use cases.

Go Command - `go run`(documentation)

Understand the functionality and usage of the `go run` command, which compiles and runs Go programs.