Simple Hello World Program

Simple Hello World Program

In this tutorial we will discuss about how to write a simple hello world program using Go language.

Simple Hello World Program

A Go program file is a simple UTF-8 text file with .go extension. Hence you can edit a Go program in simple text editors like notepadsublime text or IDEs like VSCode or WebStorm. You can use also terminal editors like vim or nano too.

Here i am using VSCode editor/IDE. This will help you write better code and make formatting/debugging easier.

A typical program has .go file extension. Now let’s create a “Hello World” program. For that first create a directory named hello outside $GOPATH/src.  Since the project is getting creating outside $GOPATH/src we also need to create a go.mod file with import path as waytoeasylearn.com/hello.

We will look at the import path, go.mod file and module in detail in the upcoming tutorial. For now, let’s create a simple module to see how the hello world program looks like in go. Use the below command for that.

go mod init waytoeasylearn.com/hello

It will create go.mod file as below.

module waytoeasylearn.com/hello

go 1.14
Go program structure

Every Go program must be included in a package. A standalone executable Go source code must have a main function and must be included in the main package. The main function is the entry point of the execution.

package main

import "fmt"

func main() {
	fmt.Println("Hello Welcome to Waytoeasylearn..!!")
}

Output

ashok@waytoeasylearn:~/work/opsramp/go$ go run hello.go
Hello Welcome to Waytoeasylearn..!!

In the above source code, we have defined the package main at the top of the file because this is our standalone executable program, and we also added main method, which will get executed when this program runs.

We are also imported the fmt (fmt is short for format) package from Golang’s standard library (it comes with Go installation). We can use the import keyword followed by the package’s name in double quotes to import a package.

Here the fmt package is used to print messages to the standard output. This package provides different functions to log any data to the standard output (STDOUT) in different formats.

In the above example, we have used the Println function, which prints arguments as strings to the standard output or terminal.

How to Run a Go program

I am assuming we have installed Go on our system and running a Go program, and we need to instruct the Go compiler to compile and run a program using the go run command with the relative or absolute path of the Go program file.

Suppose we put our previous Go program code in hello.go file, then the command to run this program from the exact file’s directory would be as below.

$ go run hello.go

Once you run the above command, you will get the following result in the terminal.

Hello Welcome to Waytoeasylearn..!!

Please note that if we want to run multiple Go files at once, we can give a glob pattern or mention all the files in just one command.

$ go run dir/*.go
$ go run dir/**/*.go
$ go run test1.go test2.go test3.go

We can run multiple Go files at once because you might have separated different parts of your project code in their own separate files. This is helpful to manage large projects and make your code modular.

However, only one main method can exist among them since there can be only one execution entry point. All program files should belong to the same directory (usually in the project’s parent directory).

To create a binary executable file, we can use the go build command.

$ go build hello.go

The above command will create a binary executable file hello in the current directory, which you can execute from the terminal/command prompt.

$ ls
hello  hello.go

$ ./hello

go run will compile and run the source code, whereas go build compiles the source code, not execute our source code.

Go CLI
  1. go build: Compiles a bunch of go source code files
  2. go run: Compiles and executes one or more files
  3. go fmt: Formats all the code in each file in the current directory
  4. go install: Compiles and installs a package
  5. go get: Downloads the raw source code of someone else’s package
  6. go test: Runs any tests associated with the current project

That’s all about the Simple Hello World Program in Golang. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Golang.!!

Simple Hello World Program
Scroll to top