Fundamentals of GO Language

Go, also referred to as Golang, is an open-source, statically typed, compiled programming language created by Google.

It is designed to be straightforward, powerful, readable, and effective.

It was created by the trio of Robert Griesemer, Rob Pike, and Ken Thompson, who reportedly all despise C++.

Go is used for cloud-based programming, game development, data science, and server-side (backend) programming. Making command-line tools is another common use for it.

Go is an explicit, statically typed programming language that is based on C. The Go language has grown to be very popular for creating microservices and other applications because of its quick startup time, low runtime overhead, and capacity to operate without a virtual machine (VM). Go is also used for concurrent programming, which is a technique for carrying out several tasks concurrently, out of order, or partially out of order.

Types in GO:

Basic Types:NumbersStringsBoolean
Composite Types:Arrays (aggregate)Structs (composite)
Reference Types:PointersSlicesMapsFunctions
Interface TypesNo Types

Basic Types in GO:

(Note: "Gofmt" automatically formats and indents code for readability)

Numbers: They are of 3 types Integers, Floating-Points, and Complex Numbers.

Strings : Represent a sequence of Unicode characters.

Boolean : Represented by either a true or false value.

Aggregate/Composite Types in GO:

Arrays:

  • Arrays in GO are distinct from those in dynamically typed languages like Python or JavaScript.

  • An array in GO is a set of identically typed objects that have a fixed size. In GO, an array also consists of adjacent memory locations, with the first element's lowest address serving as its address.

  • You declare an Array similarly to how you would declare a variable in GO and subsequently, access an element of an array in the standard, programmatic form.

Struct:

  • Similar to objects in OOP languages, a struct in Go is a data structure that stores items of various types.

  • They can be used to make models that aid in the structure of actual objects in the world.

  • 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.

Pointers:

  • A variable is nothing more than a storage location for data in memory. A pointer is a variable whose value denotes the location of another variable in space.

  • It is defined with the help of the var keyword, then an identifier set to a data type that is prefixed with an asterisk.

  • When performing operations like call by reference, pointers are essential.

Slice:

  • In Go, a slice is an abstraction that is constructed on top of an array. It permits an array to grow in size and gives access to utility functions that would otherwise be called on an array, such as len(), which returns the length of the slice, and cap(), which returns the array's capacity.

  • A slice may be declared in one of two ways. The first specifies a slice's type (int, string, or bool), followed by a pair of empty square brackets as an identifier.

  • Second one would be; Add a pair of curly braces after the slice type and enter any values you like to initialise a slice with.

  • Slices that are declared without inputs by default have a value of null.

Maps:

  • A map is a type of data that links distinctive keys to distinct values.

  • There are two ways to declare a map. The first method entails using the var keyword and setting the identifier to the keyword map followed by the type of key inside of two square brackets; outside of the brackets, specify which type of value corresponds to the key.

  • The make method can also be used to declare a map.

Functions:

  • One function with the identifier main() must be present in every GO programme, at the very least. A GO application's launcher is located here.

  • The identifier (name), return type, and parameters of a function are all specified in a function declaration, which is given to the compiler.

  • The terms "methods," "subroutines," and "procedures" can all be used to describe functions in GO.

Interfaces:

An interface is a data type that is made up of a list of method signatures that let structs of the same type access any explicitly defined functionality.

References : Google, Medium