Nhảy tới nội dung

Collection

Array

An array in Go is a fixed sized entity. All elements in an array must have same type.

// long way
var arr [3]int
arr[0] = 1
arr[1] = 2
arr[2] = 3

// short way
arr := [3]int{1, 2, 3}


> [1 2 3]

Slice

Slice is built on top of array. It is like array, but dynamically sized and offer up a lot of really neat attributes \leftarrow used more than array

Therefore, you can create slice from array by using [start:end].

arr := [3]int{1, 2, 3}
slice := arr[:] // [:] means from start to end

Slice is kind of pointer, not really, but it is pointing to underlying array. Change arr will change slice as well.

arr[1] = 42
slice[2] = 27


> arr: [1 42 27]
> slice: [1 42 27]

Otherwise, create a new slice:

slice := []int{1, 2, 3}

Using append(slice, elements...) to add new elements to slice.

slice = append(slice, 4, 5, 6)


> slice: [1 2 3 4 5 6]

Just like create slice from array, we can also create sub slices from slice using [start:end]

s2 := slice[1:]
s3 := slice[:2]
s4 := slice[1:2]


> s2: [2 3 4 5 6]
> s3: [1 2]
> s4: [2]

Map

Map represents key-value relationship. To create map, use map keyword.

m := map[string]int{ "foo" : 42 }


> m: map[foo:42]
> m["foo"]: 42

All the keys had to be the same type, same with all the values.

m["foo"] = 27
m["foo"] = "bar" // error: (type string) cannot be represented by the type int

You can delete a pair using delete(map, key).

delete(m, "foo")


> m: map[]

Struct

Go doesn't have class, but it has struct.

This is the only collection type that allows associate disparate data types together.

In struct, field is fixed at compile time. To declare a struct:

type user struct {
ID int
FirstName string
LastName string
}

To add the value to field:

var u user


> u: {0 } // struct initialized with 0 value (0 in int is 0, 0 in string in blank)
u.ID = 1
u.FirstName = "Huy"
u.LastName = "Tu"


> u: {1 Huy Tu}

Note that the fields are fixed at compile time, means it cannot be dynamically added or removed.

There's also an implicit way to declare struct:

u := user{ID: 1, FirstName: "Huy", LastName: "Tu"}


> u: {1 Huy Tu}
warning

If you use multiple lines declaration, you will need to add a comma at the end.

Below declaration will get error:

u := user{ ID: 1,
FirstName: "Huy",
LastName: "Tu" // error: Need a trailing comma before a newline in the composite literal
}