First Program

Classic Hello World Program in Go

Okay, so we are all set up with Go. Now let's create our hands dirty and create a trivial Hello World program. Let's create a file and it hello.go

package main
import "fmt"
func main() {
    fmt.Println("Hello World")
}

That's it. We are done with our program and now save it in a file named hello.go Now Go provides us to compile and run the program in a single command. Fire up the terminal, head in a folder where we saved hello.go and run

Note: For example code shown here refers to go environment in Ubuntu 18.04. Illustration may change a bit depending upon OS being used

go run hello.go
# => Hello World

This should output the > Hello World string. Go also provides us a way to compile this program into executables by using go build

go build hello.go
# => This should create an executable in same directory
# => hello  hello.go

Is hello is an executable generated by go build. This can be run in terminal

./hello
# => Hello World

Last updated