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 strings
package, 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 int
s means 0
s.
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 string
s of length 3
(initially zero-valued).
1 | s := make([]string, 3) |
We can set and get just like with arrays.
1 | s[0] = "a" |
Slices can be dynamic. For example, to append, use builtin append()
:
1 | s = append(s, "d") |
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 | m["Key1"] = 7 |
Use name[key]
to get a value for a key with name[key]
.
1 | v1 := m["Key1"] |
The builtin delete
removes key/value pairs from a map.
1 | delete(m, "Key2") |
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 | _, isThere := m["UnkownKey"] |
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 | type Person struct { |
This defines a new type, which we can use to create new Person
objects:
1 | p := Person{name: "Alice", age: 30} |
You can access struct fields with a dot:
1 | 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 | i := 7 // declare an int variable 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.
Comments