Discover the Essentials of Go Programming in 2022
Written on
Chapter 1: Introduction to Go Programming
In 2022, embarking on a journey to learn a new programming language can be exciting and rewarding. Go, an open-source language, simplifies the development of reliable and efficient software. This article aims to provide you with a foundational understanding of the Go Programming Language.
Getting Started with Go
To begin using Go, the first step is to install it. You can find the installation guide at this link.
Writing Your First Go Code
Go organizes programs into packages, which are collections of source files located in the same directory. Any functions, types, variables, and constants defined in one source file can be accessed by other files within the same package.
For a simple program, follow these steps to set up your module path and create a ‘go.mod’ file:
$ mkdir go-project
$ cd go-project
$ go mod init go-project
This creates a new module called go-project. You can verify it by checking the contents of go.mod:
$ cat go.mod
module go-project
Next, create an app.go file in your directory, containing the following code:
package main
import "fmt"
func main() {
fmt.Println("Hello Medium")
}
You can now install this program using the Go tool:
$ go install go-project
This command compiles the go-project command and produces an executable binary.
Source Control
If you plan to use a version control system, this is an opportune time to initialize a repository, add your files, and make your first commit. However, using source control is not mandatory for writing Go code.
$ git init
Initialized empty Git repository in go-project/.git/
$ git add go.mod app.go
$ git commit -m "initial commit"
Importing Packages
To utilize external packages, you can specify how to fetch the package source code using a version control system such as Git:
package main
import (
"fmt"
"github.com/google/go-cmp/cmp"
)
func main() {
fmt.Println(cmp.Diff("Hello World", "Hello Go"))
}
When executing commands like go install, go build, or go run, the Go tool will automatically download the necessary remote module and log its version in your go.mod file.
Variable Declaration
In Go, within a function, you can use the := short assignment syntax instead of a var declaration with an implicit type:
package main
import "fmt"
func main() {
var i, j int = 1, 2
k := 3
c, python, java := true, false, "no!"
fmt.Println(i, j, k, c, python, java)
}
Go supports several fundamental data types, including:
- String: A sequence of characters
- Integer: A non-decimal number
- Float: A decimal number
- Boolean: TRUE or FALSE values
- Array: A collection of values of the same type
Basic Operators
Go includes several basic operators:
- + (Addition)
- - (Subtraction)
- * (Multiplication)
- / (Division)
- % (Modulus)
- ** (Exponentiation)
Working with Arrays
Go features a data structure known as arrays, which can hold a fixed-size sequential collection of elements of the same type.
To declare an array, use the following syntax:
var variable_name [SIZE] variable_type
For example:
var balance = [2]float32{100.22, 500.03}
Loops
Loop statements allow you to execute a block of code multiple times. Below is a simple infinite loop example:
package main
import "fmt"
func main() {
for true {
fmt.Printf("This loop will run forever.n")}
}
Recursion
A recursive function is one that calls itself. Here’s a basic example:
func recursion() {
recursion() // function calls itself
}
func main() {
recursion()
}
You can also create functions like factorial using recursion:
package main
import "fmt"
func factorial(i int) int {
if i <= 1 {
return 1}
return i * factorial(i - 1)
}
func main() {
var i int = 15
fmt.Printf("Factorial of %d is %d", i, factorial(i))
}
Wrapping Up
After reading this article, I hope you now feel equipped to write simple Go code and have a clearer understanding of what Go programming entails.
Chapter 2: Recommended Videos
Gain insight into the fundamentals of Go programming with this introductory tutorial designed for beginners.
Discover why Go is considered one of the best programming languages to learn in 2022, and how it can benefit your programming journey.