DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Hello World in Go

Hello World in Go

Go (also known as Golang) is an open-source programming language developed by Google. It is designed to be simple, efficient, and secure, making it an excellent choice for building reliable and scalable software. In this guide, we will walk you through the steps of writing and running a "Hello World" program in Go.

Prerequisites

Before we get started, ensure that you have Go installed on your machine. You can download the latest stable release from the official website at https://golang.org/dl/ and follow the installation instructions specific to your operating system.

Writing Your First Program

Now that you have Go installed, let's write our first program! Open your favorite text editor or integrated development environment (IDE), create a new file with a .go extension (e.g., hello.go), and enter the following code:

package main

import "fmt"

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

Let's quickly go through what each line in this program does:

  • The package main statement informs the Go compiler that this file belongs to the main package.
  • The import "fmt" statement imports the formatting package from the standard library. It provides functions for formatted I/O operations.
  • The func main() function is where our program starts executing.
  • Finally, within our main() function, we use fmt.Println() to print out "Hello World!" to the console.

Once you've entered this code into your file, save it.

Running Your Program

To execute your Go program, open a terminal or command prompt window and navigate to the directory where you saved your .go file.

Run the following command:

go run hello.go
Enter fullscreen mode Exit fullscreen mode

If everything goes well during compilation and execution, you should see the output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have successfully written and executed your first Go program.

Compiling Your Program

In addition to running your Go program using go run, you can also compile it into an executable binary file. This allows you to distribute and run your program on other machines without requiring Go to be installed.

To compile your hello.go file, open a terminal or command prompt window, navigate to the directory containing the file, and enter the following command:

go build hello.go
Enter fullscreen mode Exit fullscreen mode

This will generate an executable file named hello (or hello.exe on Windows) in the same directory.

To run the compiled binary, simply enter its name in the terminal or command prompt:

./hello       # Linux/macOS
.\hello.exe   # Windows
Enter fullscreen mode Exit fullscreen mode

You should see the same "Hello World!" output as before.

Conclusion

Writing a "Hello World" program is a common starting point when learning any programming language. In this guide, we walked through writing and running a simple "Hello World" program in Go. Now that you're familiar with basic Go syntax and how to execute programs, you are ready to explore more complex features of this powerful language.

Happy coding!

Top comments (0)