Go: Fundamentals

Chris Kakos
The Startup
Published in
6 min readSep 21, 2020

--

Learning a programming language takes research. Each one is merely a tool used to perform a specific task and different languages offer benefits over others which make it important to know exactly what you want to achieve before choosing an option.

Artificial Intelligence is recommended with Python because of built-in libraries like NumPy and Pandas that offer functionality for A.I. development out of the box. If you’re working with Embedded Systems, more than likely you would use a language like C due to its power and low-level access.

Now don’t get me wrong — depending on the community — you can accomplish almost anything nowadays, no matter the language chosen. There are a number of package managers composed of libraries — created by maintainers and community members — that will get the job done but to fully optimize your project, selecting the right language makes for a stronger architecture.

G o is a powerful language. It was built on the premise of efficiency and ease of programming. The syntax can be written loosely or precise as you intend. The language has differentiated itself from others with features that highlight concurrency but no matter your purpose, executes at speeds more performant than any syntax to come before it. It might not be a language that I would recommend to a beginner but I was able to grasp the concepts after learning JavaScript — although, you may want to check out TypeScript before making the leap. Nonetheless, let’s explore the basic fundamentals of Go.

Types

Types dominate the Go programming language. They specify the underlying data type of a variable. There are four different classifications of a type:

  1. Basic types
  • Numbers
  • Strings
  • Boolean

2. Aggregate/Composite types

  • Arrays — aggregate: groups values of same type
  • Structs — composite: groups values of different types

3. Reference types

  • Pointers
  • Slices
  • Maps
  • Functions
  • Channels* ( not covered )

4. Interface type

Basic Types

Numbers

Number types are separated into 3 sub-categories: Integers, Floating-Points, and Complex Numbers.

Strings

String types represent a sequence of Unicode characters.

Boolean

Boolean types are represented by either a true or false value.

Variables

There are three primary ways of declaring variables in Go. The first way is by using the var keyword before its identifier followed by the variable type. You can then set the variable to a value of your choosing. If a value is not provided, the variable will be set to its zero value.

The second way to declare a variable is by way of the shorthand syntax — which can only be used inside of a function — and uses type inference like that of a dynamically-typed programming language. Shorthand variables are declared with the := operator — which is said to resemble that of a gopher — and requires the compiler to interpret its type by the value set to it.

The third way is by using the keyword const which will set an immutable value that cannot be changed.

Functions

Each program written in Go has — at minimum — one function with an identifier of main(). This is the entry point of a Go application. A function declaration provides the compiler information about the function including its identifier ( name ), return type and parameters. Functions in Go can also be referred to as methods, subroutines, or procedures.

You can also return multiple values with functions in Go.

Arrays

Arrays in Go are different from that of dynamically types languages such as JavaScript or Python. In Go, an array is a collection of a fixed-size and its elements all share the same type. An array in Go also consists of neighboring memory locations with the lowest address corresponding to the first element. You declare an array similarly to how you would a variable in Go and subsequently, access an element of an array in the standard, programmatic form:

An array is also the underlying structure of a slice.

Slices

A slice is an abstraction that is built on top of an array in Go. It allows for an array to increment in size and can access utility functions that are otherwise called on an array like len()— to output the length of the slice — and cap() which outputs its capacity.

There are two ways to declare a slice. The first sets an identifier to a pair of empty square brackets followed by the type of slice — int, string, bool. To initialize a slice with values, add a pair of curly braces after the slice type and input whatever you wish. Slices that are declared without inputs are initialized to nil by default.

You can add items to a slice with the built-in append( ) method.

Structs

A struct in Go is a data structure that stores items of different types, similar to objects in OOP languages. They can be used to create models which help structure real world objects. To declare a struct, use the type keyword followed by its identifier, a struct statement and a pair of curly braces.

To access items from a struct, use the member access operator, which is to Go what dot notation is to JavaScript.

Maps

A map is a data type which relates unique keys to values. You can declare a map one of two ways. The first way is by using the var keyword and setting the identifier to the keyword map followed by the type of key inside of a pair of square brackets; outside of the brackets, specify which type of value corresponds to the key.

You can also utilize the make method to declare a map.

Pointers

A variable is nothing more than an address in memory that stores data. A pointer is a variable whose value points to another variable’s location in space. It is defined using a var keyword followed by an identifier set to a data type that’s preceded by an asterisk. Pointers are crucial in performing tasks such as call by reference.

Interfaces

An interface is a data type that consists of a list of method signatures that allow for structs of the same type to access whatever functionality is explicitly defined.

Conclusion

The above was meant to serve as an introduction of the syntax and to familiarize you with the data structures of the Go programming language. There are a number of resources available to learn Go, some of which I have outlined in a previous overview of the language. If you’re interested in a more official documentation, I’d recommend starting with A Tour of Go. Once you’ve taken the time to explore the core concepts, I implore you to venture out into the Standard Library to truly understand what Go has to offer.

What are you waiting for? Go!

--

--

Chris Kakos
The Startup

Software developer skilled in ecosystems surrounding JavaScript.