DEV Community

Cover image for How to Print Hello World in Go: A Beginner's Guide
Amal Satheesan
Amal Satheesan

Posted on • Updated on

How to Print Hello World in Go: A Beginner's Guide

Introduction

This post will guide you through printing the universally beloved "Hello, World!" message in Go. But before we dive into the code, let's provide a brief introduction to the Go programming language:

Go, also commonly referred to as Golang, emerged in 2009 as a statically typed, compiled programming language developed by Robert Griesemer, Rob Pike, and Ken Thompson at Google. Although it shares syntactical similarities with C, Go incorporates memory safety, garbage collection, structural typing, and CSP-style concurrency.

Go is a modern and versatile programming language, purpose-built for simplicity, efficiency, and reliability. Its capabilities span across various domains, including web development, distributed systems, command-line tools, and more.

If you've already set up your Go environment on your system, feel free to follow along. If not, don't worry; we'll also discuss setting up your Go environment.

Writing Your First Go Program

Let's embark on the journey to print everyone's favorite:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Create a Directory

Begin by creating a new directory for your Go project, giving it a name of your choice. For this example, we'll call it hello-world.

mkdir hello-world
Enter fullscreen mode Exit fullscreen mode

Navigate into your newly created directory:

cd hello-world
Enter fullscreen mode Exit fullscreen mode

Create a Go File

Inside your project directory, create a new file named main.go. You can use any text editor or integrated development environment (IDE) to create and edit the file.

Hello, World Program

Within the main.go file, insert the following code:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

Remember to save the file. That's all there is to it.

Running Your Go Program

  1. Open the terminal, either within your IDE or using your system's built-in terminal.

  2. To execute the program, utilize the go run <filename> command.

go run main.go
Enter fullscreen mode Exit fullscreen mode

You should see the following output:

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have successfully run your first Go program, printing the timeless "Hello, World!" message. This marks the beginning of your exciting journey into the Go programming language.

Stay tuned for more Go tutorials and exploration. Happy coding! 😊

Top comments (0)