Writing Hello World program has become traditionally a trend to write your first program in any programming language whether it is on python or java or anyone. It's a program that simply prints out Hello World
at your terminal.
Writing the Go program is easy as we will see here. Without further ado, let's write in Go lang.
If you know about installation or have installed Go, you can skip this.
Installation
If this is your first time setting up Go on your computer, don’t worry; installing Go is easy. All you need to go towards official documentation and follow the guide based on your local machine.
If you have already install Go, make sure you check out the version you have from your terminal by using the go version command.
go version
Also, you can run the go env
command to get the
information about your current Go installation and environment. The
output should look something like this:
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/username/.cache/go-build"
GOENV="/home/username/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/username/go"
......
Hello World
Actually, Hello world is a simple and complete first program for beginners. Create a folder called "hello" and create a file called "hello.go". You can create it at your own choice. Be prepared with your favorite IDE or text editor or any online compiler.
mkdir hello
cd hello
touch hello.go
You can try it out from the online go playground.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
You need to know that Go applications require the main package and exactly one main() function that serves as the entry point for the application. The main function takes no arguments and returns no values. Instead, it tells the Go compiler that the package should be compiled as an executable package. Let's run the Hello World program.
go run hello.go
Output
Hello, World!
It uses the fmt
package and calls the Println
function with Hello, World as an argument. import
is a go keyword that tells the go compiler which other packages you want to use in this file. You imported fmt
and it provides formatting and printing functions. fmt.Println
is a go function, found in fmt package that tells the computer to print some text to the screen.
Let's understand what's going on under the hood. First of all, you need to know what go run <filename>
does exactly. go run
combines building and running the application in one command. Try out using another command in the same program.
go build hello.go -o hello
./hello
Well, it does exactly here too. Wait what's the difference here? Here, go build
create a binary that is distributed for other people to use. Most of the time, this is what you want to do. Using the -o
flag, it gives the binary a different name or location. If we look again in the project, using go run
, it doesn't create any binary right.
Actually, it does, go run
command does, in fact, compiles your code into a binary. However, the binary is built in a temporary directory. The go run
command builds the binary, executes the binary from that temporary directory, and then deletes the binary after your program finishes. This makes the go run
command very useful for testing out small programs.
Awesome work. You just wrote your first Go program. Subscribe to the newsletter for upcoming updates related to Go lang. Thank you for reading till the end.