Hello World to Golang
Today, we will start with basic programming in Golang and see how to build and execute a Golang program.
Topics to be discussed:
- Go Commands
- Program Structure
- Execution
For installation, please refer:
Go Commands:
There is a tool called “Go” which is used to manage go source codes like building the go program, running the go program, listing the packages of go, install specific package and it can do even more…
Here is the link to get more knowledge on Go Commands.
In this blog, we will be discussing two important commands which will be really helpful to compile and run a go program.
Build:
The command is used to compile and build a program.
go build filename.go
This command internally compiles the go program and generates an executable file when we execute the program with go build command.
Generally, it is used in production environment where it builds the program and generates a go binary file and later stores it in the current directory.
Note: The build command is used to compile executable package. If it is used with reusable package, then it won’t generate any executable file out of it.
To get more information on packages, refer my blog:
Run:
This command does the job of go build and also does one more job in addition to it.
go run filename.go
You can easily find it from the name of the command itself.
Did you find the answer?
Great! You found it….
This command again builds the program and executes the program as soon as the compilation is done.
Usage:
This command is used to run a program locally and won’t be used in the production environment as it stores the executable file in a temporary directory.
Note: The run command is used to build and run the executable package. If it is used with reusable package, then it will return an error stating “package command-line-arguments is not a main package”.
Program Structure:
The structure of the go executable program is
We can split the program structure into three parts:
- Package name
- Imports
- Functions
Package name:
This is where we define the name of our package in the program. As we are starting with a basic go program, we proceed with single go file and that too is an executable package, so we define the package name as main.
Note: All the executable package name will have the package name as main.
Imports:
The section is responsible for importing packages used in our program. If we want to use any standard library like math, fmt… Then we need to import it first and then use it by referencing the package name.
In the above example, we have imported the package called fmt which helps us to format Input and Output.
For multiple imports, we follow a slightly different syntax where the imports will be wrapped with brackets:
package main
import (
"fmt"
"math"
)
func main(){
// body
}
Functions:
Here, we define the main function and generally a function is defined with the following structure
func functionName(input1 variableType, input2 variableType) (returnType1, returnType2) {
// Body
}
func is a keyword used to define a function.
functionName is the name of the function that tell what does this function do. This function name is user specific.
Input Parameter: We define the input parameters with its type. input1 is the variable name and variableType is the value type of the variable.
And the input parameters are separated by commas and wrapped with brackets.
Return: Here we define the type of return value that the function returns.
Say if there is an addition function which returns a value of type integer, then the return type is set as integer.
Execution:
To execute a program, we basically use two commands for two different use cases.
To build and execute a generated binary file as we do in production environment.
Step 1:
Assume we have a go file with filename as test.go
go build test.go
When we execute this command, it generates an executable file with same filename.
Step 2:
To run this executable file in Mac, we run this command which executes the program.
Command:
./test
Output:
Hello world!!!
To run a go program directly as we do in local environment:
To run a go program directly, we use the go run test.go command. It builds and executes the file directly.
Command:
go run test.go
Output:
Hello world!!!
Welcome to the vibrant world of Golang, where ‘Hello, World!’ is just the beginning of an incredible journey into the realms of powerful, efficient, and elegant programming. May your code always compile, your functions be optimized, and your passion for learning never cease.
Happy coding in Golang!”
“Live as if you were to die tomorrow. Learn as if you were to live forever.” — Mahatma Gandhi