How did I begin my wonderful journey with Golang?
Introduction
Created by a group of Google developers to resolve their frustration with existing programming languages and environments back in 2009, the Go programming language has risen to become one of the most lovable programming languages, according to the 2020 Stackoverflow Developer Survey (https://insights.stackoverflow.com/survey/2020#most-loved-dreaded-and-wanted). Go has been noticeably well-known to be a beautiful combination of C-languages performance, type safety and the ease of use of dynamic languages such as Javascript and Python, which promises state-of-the-art concurrency and scalability that is badly in need nowadays. Developers of Go has also managed to create a large ecosystem of toolsets in their standard library. This blog will give a quick glance at syntactical features of the language by giving examples of the features in action, in the view of a Golang beginner. More advanced features of the language will be covered in later blogs, so stay tuned!
Installation
Installation on personal computers is straightforward by following the official guide at: https://golang.org/doc/install. If installation is not preferred, it is also possible to try out the language in its official playground at: https://play.golang.org/.
An example Go program
Provided below is a simple console program that takes user input and print information related to a Person in Go language:
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
type Person struct {
id int
name string
birthday string `format:"dd/MM/yyyy"`
}
func (p Person) Age() int {
nowYear:= time.Now().Year()
birthYear, _:= strconv.Atoi(strings.Split(p.birthday, "/")[2])
age:= nowYear - birthYear
return age
}
func (p Person) String() string {
return fmt.Sprintf("Person[id=%d, name=%s, birthday=%s, age=%d]", p.id, p.name, p.birthday, p.Age())
}
func main() {
reader:= bufio.NewReader(os.Stdin)
fmt.Print("Enter ID: ")
idStr, _:= reader.ReadString('\n')
id, _:= strconv.Atoi(strings.TrimSpace(idStr))
fmt.Print("Enter Name: ")
name, _:= reader.ReadString('\n')
fmt.Print("Enter Birthday: ")
birthday, _:= reader.ReadString('\n')
person:= Person{
id: id,
name: strings.TrimSpace(name),
birthday: strings.TrimSpace(birthday),
}
fmt.Println("Created: " + person.String())
}
Save the above code in a file named main.go
. Then, to run the file, use the terminal, change directory to the current folder then execute the following command:
go run main.go
Syntactic features
Go is created by system developers who are too familiar with C language, therefore many cues are taken from the C language (including the notorious pointer notation). Of course, at my first step, there is no need (yet!) to find out about such an advanced concept, therefore I will discuss these pointers at my later steps in the journey. Stay tuned!
In this very first step, the most basic features that make up every Go program will be discussed. These include: package declaration, functions, variables, structs
- Package declaration:
package <packageName>
, in which: <packageName> only takes the name of the folder that the file is located in or main. - The main() function: The
main()
method defines the entry point of the application. It must be located in the main package, and takes no argument (like C, unlike other well-known programming languages). - Variable declarations: Either use the keyword
var
or:=
operator:
var a int // a = 0
var b, c int = 1, 2 // b = 1, c = 2, both are int
slice := make([]string, 0) // shorthand declaration and assignment
- Custom types (structs): Struct declaration in Golang is surprisingly similar to that of C language:
type Person struct {
id int
name string
birthday string `format:"dd/MM/yyyy"`
}
- Functions: A function is marked with the keyword func, similar to the
main()
function. The general syntax is:
func <funcName>(param1 type1) returnType { … }
- There are also anonymous functions that are defined as:
anon := func(number int) { ... }
- Since Go is not object-oriented language, there is no class and no method. The closest thing to a method (a function attached to a type) is method defined on structs:
func (r: ReceiverType) DoSomethingOnReceiverType() { ... }
func (r: *ReceiverType) DoSomethingOnReceiverType() { ... }
Methods defined on structs like these can either take pointer or value receiver. There are differences when the pointer receiver is used in place of value receiver, which will be discovered and discussed later in my Golang journey.
- Imports: Golang’s core is kept to the bare minimum, making the language extensible through the use of packages. Therefore, there is a need for importing necessary packages in the code:
import ( “package1” “package2” ... )
Extras
Golang is as strict about unused stuffs as your favourite buffet restaurants, charging developers by annoying errors when developers leave anything (imports, variable declarations, blah blah) unused, unlike in other languages where unused imports and declarations may negatively affect performance but hardly ever be found!
Conclusion
So far, my first steps in the wonderful journey in the Gopher’s land have left me in amazement at how modern the language is, compared to other well-known (yet relatively old!) languages. There is no hesitant of mine to taking further steps in the Golang path, so stay tuned for my subsequent blogs about this memorable journey!