Simple Go Program

Simple Go Program

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

Simple Go 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.

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..!!")


[email protected]:~/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). To import a package, we use the import keyword followed by the name of the package in double-quotes.

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 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 to run a Go program, we need to instruct the Go compiler to compile and run a program using go run command with the relative or absolute path of the Go program file.

If we put our previous Go program code in hello.go file, then the command to run this program from the same directory of this file would be as below.

$ go run hello.go

Once you run the above command you will get 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 entry point of the execution and all program files should belong to the same directory (usually in the parent directory of the project).

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 terminal/command prompt.

$ ls
hello  hello.go

$ ./hello

go run will compile and run the source code where as go build just compiles the source code, not actually execute our source code.

  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
Simple Go Program

Scroll to top