Go Basic: Understanding Strings, Arrays, Slices, Maps, Structs and Pointers in Go

Ready to dive into the world of complex data types in Go?

full lessons here👇:
https://programmerscareer.com/golang-basic-skill/

Generated by AI, there may be errors, for reference only

Topic: Strings in Go

In Go, strings are a sequence of bytes, which could potentially represent Unicode characters. A string can be created by putting text in double quotes:

1
var s string = "Hello, Golang"

Go has many built-in functions for manipulating strings in the stringspackage, such as strings.ToLower()strings.ToUpper()strings.Trim()strings.Replace(), and many more.

Topic: Arrays in Go

An array is a numbered sequence of elements of a single type with a fixed length. In Go, they look like this:

1
var a [5]int

Above, we’re declaring an array a that will hold exactly 5 integers. The type of elements and length are both part of the array’s type. By default an array is zero-valued, which for ints means 0s.

Go also provides a way to initialize an array with values:

1
b := [5]int{1,2,3,4,5}

Topic: Slices in Go

Slices are a key data type in Go, giving a more powerful interface to sequences compared to arrays. Unlike arrays, slices are typed only by the elements they contain (not the number of elements).

To create an empty slice with non-zero length, use the builtin make. Here we make a slice of strings of length 3 (initially zero-valued).

1
s := make([]string, 3)

We can set and get just like with arrays.

1
2
3
4
5
s[0] = "a"  
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s)
fmt.Println("get:", s[2])

Slices can be dynamic. For example, to append, use builtin append():

1
2
3
s = append(s, "d")  
s = append(s, "e", "f")
fmt.Println("appd:", s)

That’s the basics of slices in go.

Topic: Maps in Go

Maps are Go’s built-in associative data type (also known in other languages as hashes or dictionaries).

You can create an empty map with the make command:

1
m := make(map[string]int)

Set key/value pairs using typical name[key] = val syntax.

1
2
m["Key1"] = 7  
m["Key2"] = 13

Use name[key] to get a value for a key with name[key].

1
2
v1 := m["Key1"]  
fmt.Println("v1: ", v1)

The builtin delete removes key/value pairs from a map.

1
2
delete(m, "Key2")  
fmt.Println("map:", m)

The optional second return value when getting a value from a map indicates if the key was present in the map. This can be used to disambiguate between missing keys and keys with zero values like 0 or "".

1
2
_, isThere := m["UnkownKey"]  
fmt.Println("isThere:", isThere)

Now, moving on to Structs.

Topic: Structs in Go

Go’s structs are typed collections of fields. They’re great for grouping data elements that belong together.

Here’s an example of a struct in Go:

1
2
3
4
type Person struct {  
name string
age int
}

This defines a new type, which we can use to create new Person objects:

1
2
p := Person{name: "Alice", age: 30}  
fmt.Println(p)

You can access struct fields with a dot:

1
2
p.age++  
fmt.Println(p.age)

Topic: Introduction to Pointers

A pointer is essentially an address to a memory location. It’s a very powerful feature in Go if used wisely.

A simple example of using a pointer:

1
2
3
4
5
i := 7 // declare an int variable i  
p := &i // point to i
fmt.Println(*p) // read i through the pointer
*p = 21 // set i through the pointer
fmt.Println(i) // see the new value of i

Take a moment to internalize this. Pointers can be tricky!

中文文章: https://programmerscareer.com/zh-cn/go-basic-02/
Author: Wesley Wei – Twitter Wesley Wei – Medium
Note: If you choose to repost or use this article, please cite the original source.

Go Basic: Understanding For, If Else, Switch, Functions, Range, Closures, Recursion in Go 15 lessons help you understand Go better — Golang Basic Skill Tree

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×