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.
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:
And that’s it! Congrats, you’re a Go coder now.
Thanks for reading, and happy coding!