Go provides a set of built-in primitive types. These types are the building blocks of Go programs. The most common primitive types are:
int
, int8
, int16
, int32
, int64
uint
, uint8
, uint16
, uint32
, uint64
float32
, float64
complex64
, complex128
bool
string
byte
(alias for uint8
)rune
(alias for int32
, represents a Unicode code point)*T
(pointer to a value of type T
)When working with arrays or slices, you will need to use some functions to manipulate them. Here are some of the most common functions:
len()
: Returns the length of the array or slice.append()
: Adds elements to the end of a slice.copy()
: Copies elements from one slice to another.make()
: Creates a slice with a specified length and capacity.delete()
: Removes a key-value pair from a map.sort()
: Sorts a slice in ascending order.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:
len()
: Returns the number of key-value pairs in the map.delete()
: Removes a key-value pair from the map.make()
: Creates a map with a specified key and value type.keys()
: Returns a slice of the keys in the map.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
.
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:
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.
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
.