Run Golang Program in Linux or Windows

Writing your first program in any programming language is a tedious yet exciting task. This article discusses how to write, save, and run a golang program in Linux or windows.

Before discussing how to run a golang program, I suggest you read this article on how to install golang in Linux. If you are a windows user, you can read this article on how to install golang in windows.

After installation of the golang compiler, you can proceed with this article. If you have already installed go on your machine, let’s discuss how to write and run a go program.

How to Save a Golang Program in Linux and Windows?

To write a golang program, you first need to open a text editor such as gedit in Linux. In windows, you can use notepad or notepad++.

Next, you need to save the file with a .go extension. In Linux, you can directly name the file withe the .go extension while opening it as shown below.

Create a golang file in Linux
Create a golang file in Linux

In the above image, you can observe that we have created the file helloworld.go using the command prompt.

In Windows, you need to explicitly save the file using the .go extension. While saving, don’t forget to save the file type as All Files as shown in the following image. Otherwise, the file will be saved using .go.txt extension and our program won’t work.

Save golang program in Windows
Save the golang program on Windows

Once we have created the file, we will write the golang program and run it using the command prompt.

Write a HelloWorld Program in Golang

Now that we have you have installed the go compiler and created a file with the .go extension, you can write and run a golang HelloWorld program.

A simple golang program has three main sections.

Package Definition

This is the first statement of the source code file. In this statement, we use the package keyword to declare the package name of the file. The syntax for declaring a package in golang is as follows.

package package_name

The above statement shows that the current source file belongs to the package package_name.

Import Modules in Golang

The import section is used to import all the modules needed to run the golang program. To import a single module, you can use the following syntax.

import "module_name"

Here, the golang module module_name is imported after execution of the above statement. Instead of using the above syntax, you can also use parentheses with the import keyword to import a module in golang as shown below.

import (
       "module_name"
)

In the above syntax, observe that the module name is written in the next line of the import statement.

To import multiple modules in golang, you can use multiple import statements as shown below.

import "module_name1"
import "module_name2"
import "module_name3"

After execution of the above three statements, all the modules are imported into the current program.

Instead of using the above syntax, you can use the factored import statements to import multiple modules as shown below.

import (
	"module_name1"
	"module_name2"
	"module_name3"
)
      

The above syntax works in the same manner as the previous syntax. However, it reduces redundancy in the source code. Hence, you should use the factored import statement using the above syntax to import multiple modules in golang.

Define the main() Function

The main() function is the starting point of any golang program. To write any function in golang, we use the following syntax.

func function_name(){
function body
}

Here,

  • func is the keyword to specify that function_name() is the function.
  • The function body is enclosed inside curly brackets {}. You can write statements to execute in the function body. Each statement in a golang program ends with a newline character just like a python program.

Now that we have discussed the basic elements of a golang program, let us write a short code to run the Hello World program in golang.

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

Here, 

  • The first statement defined the package name.
  • The import statement imports the fmt module in the program.
  • Next, we define the main() function to print the hello world statement using fmt.Println() function.

After writing the program, we save it using the .go file extension. I have kept the file as helloworld.go.

Run Golang Program in Linux Ubuntu

To run the go program in Linux Ubuntu, open the command prompt in the directory where you have saved the file. Then execute the following command.

go run helloworld.go 

After entering the above command, the program will execute successfully if it contains no errors and prints the output as shown in the following image.

run go program
run go program

To run any golang program using the command line in ubuntu, you can use the following syntax.

go run <filename>

Here, the filename must end with the .go file extension.

Run Golang on Windows

To run golang in windows, you can simply open the command prompt in the same directory as the source code and use the command go run <filename> as shown below.

Run Golang Online

To run golang online, you can use the golang code playground.

Conclusion

This article discusses how to write, save, and run a golang program in Linux or windows. To uninstall golang, you can read this article on how to uninstall golang in Linux. For windows, you can read this article on how to uninstall golang in windows.

Donate to Avid Python

Dear reader,
If you found this article helpful and informative, I would greatly appreciate your support in keeping this blog running by making a donation. Your contributions help us continue creating valuable content for you and others who come across my blog. 
No matter how big or small, every donation is a way of saying "thank you" and shows that you value the time and effort we put into writing these articles. If you feel that our article has provided value to you, We would be grateful for any amount you choose to donate. 
Thank you for your support! 
Best regards, 
Aditya
Founder

If you want to Pay Using UPI, you can also scan the following QR code.

Payment QR Code
Payment QR Code

Leave a Reply