Learn Go

Learn Go

Hey there! Before diving into the topic, small heads-up is this blog and coming-up ones aren't solely intended to teach someone but to me too. Also hoping this might act as a reference to my future self. Let's begin

Intro to Go

Go programming language also known as Golang is a high-level programming language developed by Google and designed with systems programming in mind.

Its characteristics

  • Open source (free to use and even its development code can be modified)

  • Statically typed (when declaring variables its type need to be specified)

  • compiled language (code if first converted to machine code and then executed)

  • Built-in concurrency (the execution of code functions to run independently)

  • Built-in support for testing, and many more...

Topics to be covered

  • Variables

  • Constants

  • Data types

Variables

A variable is used to store some data. That data could be any type int, char, string, etc..

var variable_name datatype = expression

Ex: var name string = "Golang"

In the above code snippet, a string Golang is assigned to a variable name, so that in the later part we could just use name to access the string Golang

var name = "Golang"

We could skip providing the data types if needed as shown above.

We can even skip the keyword var and can directly assign data to the variable using the walrus operator :=. This is also known as implicit type inferencing, but this only works inside a function as shown below.

package main 
import "fmt"

func main(){
    name := "Golang"
    fmt.Println(name)  // prints Golang
}

Let's first see what every line means in the above code. The line package main says that this file(the file in which the code is written) belongs to main package.

Don't worry about packages right now, we'll get to packages in later blogs, just know it for now. The next line import "fmt" imports a package called fmt. The block func main() { ..... } is where our code to be executed is written. The variable name is assigned with Golang and the following line prints it to the console.

In Go, we use double slash // to write a comment, where comments are just for developers to write some notes. These comments are ignored when executing the code

If we want to declare multiple variables all at once, we can do so

var name, age, balance = "Golang", 13, 0.0

Scope of variables

The scope of a variable is its availability region. So variable that is out of its scope, can't be accessed to a particular region.

package main 
import "fmt"

func main(){
    if true {
        var name = "Gloang" 
        fmt.Println(name) // prints Golang
    }
    fmt.Println(name)  // raises error, undefined: name 
}

If you've observed, the variable name if confined to its if block, which means name's scope is limited to that if block and can't be accessed outside of if block.

These kinds of variables are called Local variables - variables whose scope is confined to some region of code.

Contrary to local variables we have Global variables - variables that do not have scope limitations and can be accessed anywhere in the programme.

package main 
import "fmt"

var name = "Global variable"
func main(){
    if true {
        var name = "local variable" 
        fmt.Println(name) // prints local variable
    }
    fmt.Println(name)  // prints Global variable 
}

Constants

Variables are mutable, meaning their data can be modified in the later part of the code. Whereas constants once declared can't be modified later. Their value/data is Constant throughout the code.

They are declared using a keyword called const.

package main
import "fmt"

const name string = "Global variable"
const age = 13
func main() {
    fmt.Println(name) // prints Global variable
    fmt.Println(age)  // prints 13
    age = 14          // raise error: cannot assign to age(untypes int constant 13)
}

Data Types

In Go we have basic types like

CategoryTypes
Stringstring
Booleanbool
Integerint, int8, int16, int32, int64
Unsigned integeruint, uint8, uint16, uint32, uint64, uintptr
Floatingfloat32 and float64
Complex numbercomplex64 and complex128

int

An integer is numeric representation of base 10 numbers like 0, 1, 20, 1412, etc, This int has many subtypes based on its size like int8, int16, int32, and int64, where the numbers 8, 16, 32, 64 represents the size the integer variable can hold.

For example, int8 can hold numbers ranging from -128 to 126,

int16 ranges from -32768 to 32767,

int32 -2,147,483,648 to 2,147,483,647, etc.

Also in addition to those we have unsigned integers represented with u as prefix.

uint, uint8, uint16, uint32, uint64 are unsigned integers, meaning they can only store positive integers

Note: For most cases, we could directly use int which defaults to int32 or int64 based on system architecture, unless we have a specific reason not to.

float

These are numbers with decimal values like 10.20, 14.39, 2.4 etc, float32 and float64 are used to represent a float data type.

string

Basically, a string is a sequence of characters that form a word or sentence.

Syntax: var str string = "Golang"

Here the variable name str is of type string with its value being "Golang"

bool

A bool aka boolean type is used if a variable is supposed to hold either true or false values.

Syntax: var flag bool = true

complex

Complex data types are used when performing calculations on complex numbers like 2+3i, 5+4i, etc, for complex64 and complex128.

Unless you're a math nerd or need to use these complex numbers for some specific reasons, we won't be using these.

byte

It's just an alias for int8, so instead of declaring a variable as int8, we could just use byte.

Syntax: var age byte = 10

rune

Rune is also an alias for int32 but is mainly used to represent characters in Golang.

If you see, Go uses Unicode code points to represent characters, as Go uses UTF-8 encoding.

package main
import "fmt"

func main() {
    var age int = 13
    var name string = "Golang"
    var flag bool = true 
    var decimalNum float64 = 20.145 
    var num byte = 9
    var y complex64 = -5+12i
    var runeType rune = 23098
    fmt.Printf("Type: %T Value: %v\n", age, age)
    fmt.Printf("Type: %T Value: %v\n", name, name)
    fmt.Printf("Type: %T Value: %v\n", flag, flag)
    fmt.Printf("Type: %T Value: %v\n", decimalNum, decimalNum)
    fmt.Printf("Type: %T Value: %v\n", num, num)
    fmt.Printf("Type: %T Value: %v\n", y, y)
    fmt.Printf("Type: %T Value: %v\n", runeType, runeType)
}

We even have some composite data types like array, slice, maps, structs, etc. We'll cover them in the next blog, till then, peace out!

Continue learning, here's the part-2 https://mohanj.hashnode.dev/learn-go-part-2.