Go — 1

Introduction
Golang, simply known as “Go” is a compiled programming language that was designed by Google. Go is known for being a language that is simple, reliable, and efficient. It is gaining popularity among developers for its fast performance, concurrency support, and concise context.
The official website for Go provides a playground for anyone that wants to get to know it better: Go Playground — The Go Programming Language.
However, if you want to install Go in your own PC, you can download it from its website at: All releases — The Go Programming Language and follow the installation instructions at Download and install — The Go Programming Language.
To code in Go, you can use any supported IDE (Visual Studio Code in this case with the Go extension installed: Visual Studio Code — Code Editing. Redefined) and save the files with the extension “.go” (e.g., main.go).
The First Program in Go
As always, let’s try printing the “Hello World!” statement for Go as the first step.
Comments in Go
There are two types of comments in Go:
- Single Line Comments (//)
- Multi Line Comments (/* */)
// This is singe line comment.
/*
This
is
a
multi line
comment */
Declare A Package
A package can be used to group functions and / or group all the files in the same directory.
// The first program in Go.
package main
Import Library Packages
To use functions for formatting text, a standard library package of the name “fmt” will have to be imported to the file.
// The first program in Go.
package main
import "fmt"
Implement the Main Function
Implement a function that will contain the output statement. A “main” function executes by default when you run the main package.
// The first program in Go.
package main
import "fmt"
func main() {
}
Add the Output Statement
To print a statement, include the output statement within the main function.
// The first program in Go.
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Given below are the output statements that you can use:
fmt.Print Print the output to the console without a newline ("\n")
character appended at the end
fmt.Printf Used for formatted printing
fmt.Println Print the output to the console with a newline ("\n")
character appended at the end
Initialize the Modules
Before running the file, initialize the modules for easier management of dependencies. This could be done by typing the following command in a command prompt:
go mod init github.com/your_username/your_file_name
Run the File
- To run the file, open a command prompt and navigate to the file location.
- Type either of the following into the command prompt:
go run main.go (go run file_name)
go run . (run the first Go file that it finds)

Declaring Variables
A variable can be used to store a value. In Go, variables can be declared as follows:
// The first program in Go.
package main
import "fmt"
func main() {
// Print a single line
fmt.Println("Hello World!")
// Print an empty line
fmt.Println()
// Declare variables to store a string and a number
name := "Rachael"
age := 22
}
Printing Variables
Variables in Go can be printed as follows once declared:
// The first program in Go.
package main
import "fmt"
func main() {
// Print a single line
fmt.Println("Hello World!")
// Print an empty line
fmt.Println()
// Declare variables to store a string and a number
name := "Rachael"
age := 22
// Print the variables
fmt.Println("Name of Programmer: ", name)
fmt.Println("Age of Programmer: ", age)
}

You can access the source code at: Go/Part-1 at master · Himashi-Karunathilake/Go (github.com)
Link to Part 2: Go — 2. Given below is the main.go file that… | by Himashi Karunathilake | Jul, 2023 | Medium