basics

Contents

Roadmap info from roadmap website

Learn the Basics

Learn the common concepts of Go like variables, loops, conditional statements, functions, data types, and so on. A good starting point for go basics is its Go’s official docs.

Visit the following resources to learn more:

What is Go

  • Go is a cross-platform, open source programming language
  • Go can be used to create high-performance applications
  • Go is a fast, statically typed, compiled language known for its simplicity and efficiency
  • Go was developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson in 2007
  • Go’s syntax is similar to C++

What is Go Used For

  • Web development (server-side)
  • Developing network-based programs
  • Developing cross-platform enterprise applications
  • Cloud-native development

Why Use Go

  • Go is fun and easy to learn
  • Go has fast run time and compilation time
  • Go supports concurrency
  • Go has memory management
  • Go works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)

Go Compared to Python and C++

GoPythonC++
Statically typedDynamically typedStatically typed
Fast run timeSlow run timeFast run time
CompiledInterpretedCompiled
Fast compile timeInterpretedSlow compile time
Supports concurrency through goroutines and channelNo built-in concurrency mechanismSupports concurrency through threads
Has automatic garbage collectionHas automatic garbage collectionDoes not have automatic garbage collection
Does not support classes and objectsHas classes and objectsHas classes and objects
Does not support inheritanceSupports inheritanceSupports inheritance

Formatting Verbs for Printf()

Go offers several formatting verbs that can be used with the Printf() function. General Formatting Verbs. The following verbs can be used with the integer data type:

VerbDescription
%bBase 2, binary
%dBase 10, decimals
%+dBase 10 and always show sign
%oBase 8
%OBase 8, with leading 0o
%xBase 16, lowercase
%XBase 16, uppercase
%#xBase 16, with leading 0x
%4dPad with spaces (width 4, right justified)
%-4dPad with spaces (width 4, left justified)
%04dPad with zeroes (width 4)

String Formatting Verbs

The following verbs can be used with the string data type:

VerbDescription
%sPrints the value as plain string
%qPrints the value as a double-quoted string
%8sPrints the value as plain string (width 8, right justified)
%-8sPrints the value as plain string (width 8, left justified)
%xPrints the value as hex dump of byte values
% xPrints the value as hex dump with spaces

Boolean Formatting Verbs

The following verb can be used with the boolean data type:

VerbDescription
%tValue of the boolean operator in true or false format (same as using %v)

Float Formatting Verbs

The following verbs can be used with the float data type:

VerbDescription
%eScientific notation with β€˜e’ as exponent
%fDecimal point, no exponent
%.2fDefault width, precision 2
%6.2fWidth 6, precision 2
%gExponent as needed, only necessary digits

Memory Efficiency

When using slices, Go loads all the underlying elements into the memory. If the array is large and you need only a few elements, it is better to copy those elements using the copy() function. The copy() function creates a new underlying array with only the required elements for the slice. This will reduce the memory used for the program.

package main

import (
 "fmt"
)

func main() {
 // Suppose we have a large array
 largeArray := [1000000]int{}
 for i := 0; i < len(largeArray); i++ {
  largeArray[i] = i
 }

 // We only need a small slice of this large array
 smallSlice := largeArray[100:200]

 // This smallSlice still references the entire largeArray in memory
 fmt.Println("Length of smallSlice:", len(smallSlice))
 fmt.Println("Capacity of smallSlice:", cap(smallSlice)) // Large capacity indicates underlying array is still large

 // To optimize memory, create a new slice with a new underlying array using copy
 optimizedSlice := make([]int, len(smallSlice))
 copy(optimizedSlice, smallSlice)

 // Now, optimizedSlice is independent of largeArray and uses less memory
 fmt.Println("Length of optimizedSlice:", len(optimizedSlice))
 fmt.Println("Capacity of optimizedSlice:", cap(optimizedSlice)) // Capacity now matches the length, indicating efficient memory usage
}

Defer

Go’s defer statement schedules a function call (the deferred function) to be run immediately before the function executing the defer returns. It’s an unusual but effective way to deal with situations such as resources that must be released regardless of which path a function takes to return. The canonical examples are unlocking a mutex or closing a file.

Golang Types and Their Explanations

TypeDescription
boolA boolean type representing true or false.
stringA sequence of characters. Strings in Go are immutable.
intA signed integer type, with platform-specific size (either 32 or 64 bits).
int8A signed 8-bit integer (-128 to 127).
int16A signed 16-bit integer (-32,768 to 32,767).
int32A signed 32-bit integer (-2,147,483,648 to 2,147,483,647).
int64A signed 64-bit integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).
uintAn unsigned integer type, with platform-specific size (either 32 or 64 bits).
uint8An unsigned 8-bit integer (0 to 255).
uint16An unsigned 16-bit integer (0 to 65,535).
uint32An unsigned 32-bit integer (0 to 4,294,967,295).
uint64An unsigned 64-bit integer (0 to 18,446,744,073,709,551,615).
uintptrAn unsigned integer large enough to store the uninterpreted bits of a pointer value.
byteAn alias for uint8, typically used to emphasize that the value is a raw byte.
runeAn alias for int32, represents a Unicode code point.
float32A 32-bit floating-point number.
float64A 64-bit floating-point number.
complex64A complex number with float32 real and imaginary parts.
complex128A complex number with float64 real and imaginary parts.
arrayA fixed-size sequence of elements of a single type.
sliceA dynamically-sized, flexible view into the elements of an array.
mapAn unordered collection of key-value pairs.
structA collection of fields, which can be of different types. Used to create complex data types.
interfaceAn abstract type that specifies a set of method signatures.
chanA channel type used for communication between goroutines.
funcA type for function literals. Functions are first-class citizens in Go.

Interfaces

  • An interface type is defined as a set of method signatures.
  • A value of interface type can hold any value that implements those methods.
#ready #online #reviewed #go #summary #informatic #data-structure #data-representation #types #syntax #functions #loops #errors