Golang also called Go language, is a popular robust programming language introduced by Google in 2007. The format of Go and C programming languages is similar, but it provides additional features like automatic garbage collection, type safety, and memory management. It is an efficient programming language for building modern large, and flexible software systems.
How to Write Go Code
Go programs are organized in packages, and a package is the set of source files kept in the same directory.
To write a Go code, you can use the online compilers or install Go on your system from the step-by-step guidelines given below:
Step 1: Download the Go source installation file according to your system from the official link:
Note: Ensure downloading the latest version of Go for your system for better performance and security.
Step 2: Once the file is downloaded on a Windows system, open the file and follow the onscreen instructions to install it.
Step 3: Verify the installation of Go via the below command:
Note: You can open Command Prompt on Windows system from the search bar. Further, we are providing the installation steps for Windows systems only.
Linux users can install Go on the terminal by running the following command:
If you are a macOS user, you can install Go by following the guidelines here.
Step 4: Next, open any text editor on your system and write your Go code.
I am writing the code in Notepad, which you can open from the start menu:
The following is the sample Go code we are using as an example:
import "fmt"
func main() {
fmt.Println("Hello and Welcome to Golang Tutorial!")
}
In the above code:
- The first line is the main package of the program, which is compulsory to write. Here Package is the Go keyword that describes from which bundle the written file belongs to and only one package is allowed per folder. The above code belongs to the package main.
- The second statement in the code is import fmt, which is the compulsory command for compiling the files of the program. Here import is also the Go keyword, and we are using the fmt package that comes from the standard library.
- Next, the main function in the above code is the start for the execution of the Go program
- fmt.println() is the Go function that is used for printing statements on the screen. The fmt.println() is followed by the sequence of characters enclosed in the double quotation that needs to be printed on the screen.
Step 5: Once the code is added, save the file on your system.
Note: You can also create a separate folder on your system and add all your Go code inside it for effective coding.
Step 6: Open the command prompt on your system and execute the below-given command to run the above code:
Ensure providing the correct path for the Go code while running the above command.
Here I am executing the run command to display the output of the sample.go file that I have created:
For some codes, you may require creating a Go module (a collection of go related packages) inside the project directory. This will be required for large projects or those that have dependencies issues. You can create a Go module by running the following command:
Then use the following command to build the project.
Once this build is completed, you can run the EXE file on the system.
In this way, you can write your Go programs on your system.
Bottom Line
Go programming language that offers efficient performance, garbage collection, memory management, and type safety for building modern and large software systems. By following the simple steps outlined in this guide, you can easily write and run Go code on your system using a text editor and the command prompt. Creating a Go module may also be necessary for large projects with dependencies, which can be easily done with a single command.