Write Hello World app in Go

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


Hello Gophers!

Today, we’re going to walk through how to create and run a “Hello, world!” app in Go. This is a great first step for anyone who is new to the language or wants to get a quick taste of what Go can do.

Why “Hello World”: The Foundation of Programming

While the “Hello World” program itself may seem trivial, it serves as a fundamental stepping stone in the world of programming.

Here’s why:

  • Simplicity: It demonstrates the core concept of printing text to the console, which is a common first step in understanding how to interact with a programming language.
  • Accessibility: “Hello World!” programs are simple and straightforward, making them easy for anyone to run regardless of their system’s resources. This allows learners to focus on the code itself, rather than worrying about compiling or running environments.

First things first, let’s create a new file and call it main.go. In Go, all executable programs must have a main package, so we’ll start by declaring that at the top of our file:

package main

Next, we’ll import the fmt package, which contains functions for formatting and printing output:

import "fmt"

Now, let’s write our main function. This is the entry point of our program, and it’s where we’ll print our “Hello, world!” message:

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

The Println function takes a string argument and prints it to the console with a newline character at the end. In this case, we’re simply passing the string “Hello, world!”.

Now that we’ve written our program, it’s time to compile and run it. To do this, we’ll use the Go command-line tool. Open your terminal and navigate to the directory where you saved the main.go file.

Then, type the following command to build and run the program:

go run main.go

You should see the message “Hello, world!” printed in your terminal.

And that’s it! You’ve just created and run your first Go program. Congratulations!

In this tutorial we:

  • Created a new file called main.go
  • Declared the main package at the top of the file: package main
  • Imported the fmt package: import “fmt”
  • Wrote the main function: func main() { fmt.Println(“Hello, world!") }
  • Compiled and ran the program with the command: go run main.go

And that’s it! Congrats, you’re a Go coder now.

Why it matters:

The fmt.Println function is a powerful tool for debugging and understanding your code. By printing the values of variables and expressions at different points in the code, you can see what’s happening as the program runs and this helps to identify issues and track the flow of data.
You can use it to:

Print debug information: Displaying the values of variables during execution allows you to understand the state of your program at any point.

Provide output to the user: This function lets you display results or messages for users.

Display information: The fmt package is used for various tasks in Go, including:
* Printing “Hello, World!”
* Displaying program output.



Stay up to date on the latest in Coding for AI and Data Science

Intuit Mailchimp