Golang by Example

Logo

Primitives

Go provides a set of built-in primitive types. These types are the building blocks of Go programs. The most common primitive types are:

Scalar Types

Compound Types

When working with arrays or slices, you will need to use some functions to manipulate them. Here are some of the most common functions:

If it's the first time you code, index 0 is the first element of the array or slice, and index 1 is the second element. The last element of the array or slice is len(array) - 1. You can also use negative indexes to access elements from the end of the array or slice. For example, array[-1] will give you the last element of the array.

Likewise, when you're working with a map, you're gonna need to use some functions to manipulate it. Here are some of the most common functions:

Pointer

For those of you who are new to programming, the concept of packages and pointers may seem a bit challenging. Pointers in Go allow you to reference a memory location instead of the actual value. Let's visualize this with a simple illustration:

         +-------+      +-------+
         |   a   |      |   p   |
         |       | <--- |       |
         |  42   |      |  &a   |
         +-------+      +-------+

In this example, a is a variable that holds the value 42, and p is a pointer that holds the address of a. When you dereference the pointer p, you can access the value stored in a. Notice that the syntax for dereferencing a pointer is *p, which means "get the value at the address stored in p. You can also use * notation to do value assignment to p. Vice versa, if you want to get the address of a variable, you can use the & operator. For example, &a will give you the address of a.

💡 Quiz
What is the new value of a after executing *p = 100`?

Nil

In Go, nil is a special value that represents the absence of a value. It can be assigned to pointers, slices, maps, channels, and interfaces. When you declare a variable without initializing it, it will have the zero value for its type. For example, an uninitialized pointer will be nil, an uninitialized slice will be nil, and an uninitialized map will be nil. The use of nil is important for error handling and checking if a variable has been initialized. For example, if you try to dereference a nil pointer, it will cause a runtime panic.

One thing to note is that the most of scalar types in Go have a default value. For example, the zero value of an int is 0, the zero value of a float64 is 0.0, and the zero value of a string is an empty string "". Which means scalar never comparable to nil. However, the zero value of a pointer is nil, which means it doesn't point to any memory location. This is different from other languages like C or C++, where uninitialized pointers can point to random memory locations.

To check if a pointer is nil, you can use the if statement:

Conversion

Sometime you may need to convert a value from one type to another or so-called type-casting. In Go, you can use the T(value) syntax to convert a value to type T. For example, you can convert an int to a float64 using float64(a), where a is an integer.

💡 Quiz
What is the result of a + b?

Reflect

In Go, the reflect package provides a way to inspect the type of a variable at runtime. This can be useful for debugging or when you need to work with types that are not known until runtime. The reflect package provides several functions and types for working with reflection, including TypeOf, ValueOf, and Kind.


Table of contents

Hello World - Begin with the classic Hello World program
Primitives - Learn about the basic data types in Go
Flow Control - Controlling the flow of your program
Struct - All about struct
Functions - Define and call functions
Methods and Interfaces - Methods and interfaces in Go
Error Handling - Handling errors idiomatically
Concurrency - Goroutines and channels
Anti Patterns - Anti patterns in Go
Libraries - Standard library and third-party libraries
Testing - Writing unit tests with `go test`
Benchmarking - Performance benchmarks
Containerization - Dockerize your Go app
What's Next? - Explore the next steps in your Golang journey


Share to: