Skip to main content

Command Palette

Search for a command to run...

Getting Started with Go: Your First "Hello, World!" Program

Updated
3 min read
Getting Started with Go: Your First "Hello, World!" Program
D

I decided to take a career break and try out building apps using Generative AI. I am also learning NextJS and developing LaunchStack, starter code which I will use for my web projects

Welcome to the world of Go, also known as Golang! If you're just starting out, you've picked a fantastic language to learn. Go is simple, fast, and designed with modern programming needs in mind. In this blog post, we'll walk through creating and understanding your first Go program: "Hello, World!". Let's dive in!

What is Go?

Go is a statically typed, compiled programming language designed at Google. It has a clean syntax, efficient execution, and a robust standard library, making it ideal for developing reliable and efficient software.

Use Go Playground for a start

So you don't get overwhelmed since we are just starting, let's not install anything to start learning Go. Let's just use the Go Playground to execute this code.

Writing Your First Go Program

Now, let's write the "Hello, World!" program. Copy this code in the Go Playground. Feel free to change the text and copy paste it.

package main

import "fmt"

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

Let's break down each part of this code to understand what it does.

1. Package Declaration

package main

Every Go program starts with a package declaration. A package is a way to group related Go code files together. The main package is special because it's the starting point of a Go program. When you run a Go program, the code in the main package is executed first.

2. Importing Packages

import "fmt"

The import statement is used to include other packages in your program. In this case, we're importing the fmt package, which stands for "format". The fmt package provides functions for formatting text, including printing to the console.

3. The main Function

func main() {

In Go, the main function is the entry point of your program. When you run your Go program, execution starts from the main function. Every executable Go program must have a main package and a main function.

4. Printing to the Console

    fmt.Println("Hello, World!")
}

Within the main function, we call fmt.Println, a function provided by the fmt package, to print the string "Hello, World!" to the console. Println stands for "print line" and prints the text followed by a newline character, so the cursor moves to the next line after printing.

Running Your Go Program

To run your "Hello, World!" program, just click Run in the Go Playground.

You should see the output below the code editor:

Hello, World!

Congratulations! You've just written and executed your first Go program.

Let's recap what we've learned:

  1. Package Declaration: Starts with package main.

  2. Importing Packages: Uses the import statement to include standard libraries like fmt.

  3. The main Function: The entry point of the program where execution begins.

  4. Printing to the Console: Uses fmt.Println to print text.

This "Hello, World!" program is a simple yet powerful example of how Go programs are structured. As you continue to learn Go, you'll discover its many features and capabilities. For now, you've taken your first step into learning Go.

Feel free to share your experiences or ask any questions in the comments below.

26 views

Learn Go

Part 2 of 3

In this series, our goal is to get you up and running in Go. You'll learn about Go basics and fundamentals. Don't worry if you don't have any programming experience because Go is very easy to learn.

Up next

Teaching Go and AI - Exciting Times!

Hi everyone! I am really happy that my recent post for my pivot about doing more content on building apps with Go and Generative AI is well-received. I already got a few messages from people asking how they can learn Go. This is getting me really exc...

More from this blog

Discovering Tech and AI

35 posts

I'm Melvin aka DonvitoCodes on social media, an AI practitioner and software developer. With my growing experience in AI development and implementation, I am eager to share my knowledge about AI.