Go is one of the modern programming languages gaining traction on many surveys as developers preferred language. Go has an easy-to-use and understand syntax while providing top-tier performance.

You can use Go to build various applications, from web apps to command-line apps, cloud services, and networking. Go’s ecosystem of third-party packages supports many other use cases.

Go has most of the features you’d expect in a modern language: generics, concurrency, type inference, garbage collection, and many more.

Getting Started With Go

Go homepage

You can run Go on most operating systems. Head to the installations page and download a preferred Go version for your operating system.

Once you’ve downloaded and installed Go, you can start writing and running Go code in files with a .go file extension.

You’ll find Go has most of the features and much of the functionality of other programming languages. If you have previous programming experience, you should find Go straightforward.

Variables in Go

Go is quite expressive on the fundamental level. There are two ways you can declare variables in Go. You can use the var keyword to declare variables of various data types. After specifying the identifier, you’ll have to set the variable's data type.

        var a string
var b int
var c any
 
var age string = "five years old"
var age = 5 // equivalent to var age int = 5
fmt.Println(age)

You can use any as the data type if you’re uncertain of the variable's data type.

You can also declare constants with the const keyword in a similar fashion to declaring variables.

        const age = "five years old"

It is impossible to modify constants after their declaration.

Go provides an alternative way to declare variables in functions. Note the use of a colon before the equals sign:

        func main() {
    age := "five years old" // equivalent to var age = "five years old"
}

Conditionals in Go

Go also has conditionals for decision-making in programs. You can use the if and else statements in your code to handle decisions.

Here’s an example if statement that compares two integers for equality:

        var a int = 12
 
if a == 12 {
    fmt.Println("a equals twelve")
}

You can only use else statements after specifying an if statement, and you have to specify the else block after the closing if block:

        var a int = 12
 
if a == 12 {
    fmt.Println("a equals twelve")
} else {
    fmt.Println("a does not equal twelve")
}

The else block runs only when the if statement evaluates to false. Go doesn’t provide else-if statements, but you can use switch statements for complex conditional statements.

Here’s the anatomy of a switch statement in Go.

        age := 7
    switch age {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    default:
        fmt.Println("zero")
}

You can create switch statements with the switch keyword, after which you can specify different cases with the case keyword. You can handle the default case using a default keyword.

For Loops in Go

Go provides for-loops for repetitive tasks, and unlike most languages, there’s no while or do-while loop in Go.

You can use the popular C-style for-loop or the range for-loop that certain data structures support.

Here’s an example of using the C-style for loop in a Go program:

        func printer() {
    for i := 0; i <= 3; i++ {
        fmt.Println(i)
    }
}

You can use Go’s built-in range for-loop on compound data structures like slices, maps, and arrays. The range function returns the index and element of the index as it traverses the data structure.

        for index, value := range dataStructure {
}

Arrays in Go

Arrays are one of the compound data types in Go. Go arrays are similar to C-style arrays and have a definite length on declaration and instantiation.

Here’s how you can declare arrays in Go:

        var arr [5]string

arr := [7]int{0, 1, 2, 3, 4, 5, 6}

You can use indexing to access, assign and update elements in a position of the array:

        arr[3] = "three"

The code above updates or inserts the string as the fourth entry of the arr array variable.

Slices in Go

Go provides slices as an alternative to arrays for dealing with data of indefinite length. Slices are similar to arrays, except that you can change the length of slices.

You’ll need to use the built-in make function to create a slice. Pass in the slice’s data type and initial length to the make function.

        slice := make([]string, 9)
slice[2] = "two"

You can use the append function to insert elements into slices. By default, the append function inserts elements at the end of the slice.

        slice = append(slice, "ten")

Append operations on slices can be expensive to work with since Go creates a new array on each append operation.

Maps in Go

Maps are the built-in associative (key-value pair) data type in Go. You can use the make function to create a map or a simple declaration where you’ll have to instantiate the map.

        maps := make(map[string]int) // using the make function
maps := map[string]int{"one": 1, "two": 2, "three": 3} // declaring & instantiating a map

You can access values in a map by specifying the keys. Also, you can insert values into a map by specifying a key-value pair.

        maps["one"] = 1 // inserting to the map
one := maps["one"] // accessing element from the map

You can use the delete function to remove key-value pairs from maps. The delete function takes in the map’s identifier and the key of the pair you want to remove:

        delete(maps, "one")

Functions in Go

Functions are the tool for code reusability in Go. You can declare functions with the func keyword followed by the function’s identifier:

        func main() {
}

Functions can accept arguments and return values. You’ll have to specify the argument type alongside the identifier for arguments.

        func add (x string, y int) int {
    return x + y
}

You’ll specify the return type before the function’s code block and return a value of the specified type for functions that return values.

Structs in Go

Go isn’t an object-oriented language by design, but you can implement object-oriented features in Go using structs.

Structs are user-defined types for grouping other data types into a single entity. Go structs can hold values of any Go-supported types, and functions can implement structs.

Here’s how you can declare a struct in Go:

        type Game struct {
    Name string
    Year int
    PlayTime float64
    Players any
    Countries map[string]string
}

The Game struct has fields with types of map, string, integer, and floating point. You can instantiate structs with default values or assign values to them.

        var mko Game // default value instantiation
 
// instantiating with values
mko := Game{
    Name: "Value",
    Year: 1231,
    PlayTime: 1345412,
    Players: [2]string{"9", "stuff"},
    data: map[string]int{"one": 1, "two": 2, "three": 2},
}

Functions can implement and access struct types. You’ll have to specify the struct parameter before the function’s identifier.

        func (g Game) FindGame(name string) {
    a := g.Year // accessing struct fields
    var b = g.Countries // accessing struct fields
}

When you pass a struct to a function, the function has access to the struct’s fields, and the function becomes a struct method.

Go Has Many Use Cases

You’ve learned the basics of the Go programming language and the syntax to start writing Go programs.

There are many fields and applications where you can use Go. Go is popularly used as a server-side language, and you can always explore building web-based applications with it too.