Hello!
The topic of today’s post is about maps and structs. Lets start start with maps
Maps
Create map
Maps is a data structure in which data is specified in pairs, keys, and values. In golang it is created as follows. map[KEY_TYPE]VALUE_TYPE
.
import "fmt"
func main() {
studentsGrades := map[string]int{"John": 50, "Alex": 70}
fmt.Println(studentsGrades)
}
$ go run main.go
map[Alex:70 John:50]
Another option is to create a map using the make keyword
import "fmt"
func main() {
studentsGrades := make(map[string]int)
fmt.Println(studentsGrades)
}
Add to map
A new element is added to the map as follows MAP_NAME[KEY] = VALUE
package main
import "fmt"
func main() {
studentsGrades := map[string]int{}
studentsGrades["Susan"] = 99
fmt.Println(studentsGrades)
}
$ go run main.go
map[Susan:99]
If the map is created using the make keyword, you can add elements to it in the same way.
Delete from a map
The element can be deleted using the delete method, delete(MAP_NAME, KEY)
package main
import "fmt"
func main() {
studentsGrades := map[string]int{"John": 56, "Alex": 67}
studentsGrades["Susan"] = 99
fmt.Println(studentsGrades)
delete(studentsGrades, "Alex")
fmt.Println(studentsGrades)
}
$ go run main.go
map[Alex:67 John:56 Susan:99]
map[John:56 Susan:99]
Get element from the map
You can get the value of the key in this way MAP_NAME[KEY]
.
package main
import "fmt"
func main() {
studentsGrades := map[string]int{"John": 56, "Alex": 67}
fmt.Println(studentsGrades["John"])
}
$ go run main.go
56
But if we try to specify a key that does not exist, we will not get an error but will get a null value
package main
import "fmt"
func main() {
studentsGrades := map[string]int{"John": 56, "Alex": 67}
fmt.Println(studentsGrades["Susan"])
}
$ go run main.go
0
To check whether the key exists, you can use the following construction VARIABLE, ok := MAP_NAME[KEY]
. The ok
variable will be true/false depending on whether the key exists.
package main
import "fmt"
func main() {
studentsGrades := map[string]int{"John": 56, "Alex": 67}
susanGrade, ok := studentsGrades["Susan"]
fmt.Printf("Susan, %v %v\n", susanGrade, ok)
johnGrade, ok := studentsGrades["John"]
fmt.Printf("John, %v %v\n", johnGrade, ok)
}
$ go run main.go
Susan, 0 false
John, 56 true
Struct
A struct is a collection of fields that can be of different types.
Create struct
Struct is created like this type NAME struct {FIELDS}
.
package main
import "fmt"
type Student struct {
Name string
LastName string
Grade int
Subjects []string
}
func main() {
studentJohn := Student{
Name: "John",
LastName: "Snow",
Grade: 90,
Subjects: []string{"Algebra", "Geometry "},
}
fmt.Println(studentJohn)
}
go run main.go
{John Snow 90 [Algebra Geometry ]}
You can also refer to a specific field of struct separately STRUCT_NAME.FILED_NAME
fmt.Println(studentJohn.Name)
$ go run main.go
John
Embedding
Go does not have an inheritance as in traditional OOP languages. But go uses composition. Let’s review how it can be used
package main
import "fmt"
type Animal struct {
Name string
}
type Bird struct {
Animal
Speed int
}
func main() {
kiwi := Bird{}
kiwi.Name = "Kiwi"
kiwi.Speed = 30
fmt.Println(kiwi.Name)
}
$ go run main.go
Kiwi
But unlike inheritance here Bird is not an Animal, but Bird has an Animal.
If you need to create an instance of the structure as in the previous example, when we initialize the fields at the time of creation, it will look like this
package main
import "fmt"
type Animal struct {
Name string
}
type Bird struct {
Animal
Speed int
}
func main() {
kiwi := Bird{
Speed: 80,
Animal: Animal{
Name: "Kiwi"
}}
fmt.Println(kiwi.Name)
}
Anonymous struct
You can create anonymous structures without first creating a structure type with a field definition
package main
import "fmt"
func main() {
kiwi := struct {
name string
speed int
}{
name: "Kiwi",
speed: 80,
}
fmt.Println(kiwi.name)
}
$ go run main.go
Kiwi
Such structures are convenient to use to organize data that does not long live in the program.
Copy struct
If you take a structure and assign it to another variable, a copy of the structure will be created
package main
import "fmt"
func main() {
kiwi := struct {
name string
speed int
}{
name: "Kiwi",
speed: 80,
}
b := kiwi
b.name = "Emu"
fmt.Println(kiwi)
fmt.Println(b)
}
$ go run main.go
{Kiwi 80}
{Emu 80}
To pass the same structure, you need to add the symbol &
package main
import "fmt"
func main() {
kiwi := struct {
name string
speed int
}{
name: "Kiwi",
speed: 80,
}
b := &kiwi
b.name = "Emu"
fmt.Println(kiwi)
fmt.Println(b)
}
$ go run main.go
{Emu 80}
&{Emu 80}