Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays, Slices, and more. Structs  A struct is a collection of fields  The keyword type is used to declare the type of the struct  Syntax: type Vertex.

Similar presentations


Presentation on theme: "Arrays, Slices, and more. Structs  A struct is a collection of fields  The keyword type is used to declare the type of the struct  Syntax: type Vertex."— Presentation transcript:

1 Arrays, Slices, and more

2 Structs  A struct is a collection of fields  The keyword type is used to declare the type of the struct  Syntax: type Vertex struct { X int Y int }

3  To access a field within a struct, use a dot  Syntax: func main() { v := Vertex{9,5} //declare and initialize v v.X = 4 //set X equal to 4 fmt.Println (v.X) //print out X }

4 Struct Literals  You can initialize the values of the fields within the struct when you initialize the struct red := Vertex{12,12} // X and Y equal 12  You can use the variableName: to specify a particular value yellow := Vertex{Y: 66, X: 12} //Y equals 66, X equals 12, order does not matter  Or you can implicitly assign values blue := Vertex{} //X and Y are both implicitly assigned 0

5 Maps  A map matches keys to values  Maps are created with make before they are used  Syntax: var myMap map [string] Vertex func main (){ myMap = make(map [string] Vertex) }

6  Assign a key to a value like this: myMap [“Red”] = Vertex{12, 12}  Map literals are like struct literals, but the keys are required var myMap = map [string] Vertex{ “Red”: Vertex {12, 12}, “Yellow”: Vertex {12, 66}, }  Alternatively, if the top-level type is just a type name, you can omit it from the elements of the literal var myMap = map [string] Vertex{ “Red”: {12,12} “Yellow”: {12,66} }

7 Arrays  Arrays are structures that store one or more values linearly  Its values are accessed by integer indices from 0 to len(arrayName) – 1  The len(arrayName) function is built in and returns the length of the passed in array  Syntax: myArray := [5] int {1,2,3,4,5};  The size of an array is included as part of its type. So [2] int is a different type than [5]int.

8  Arrays in Go are different from arrays in C Arrays are values. Assigning one array to another copies all its elements. Passing an array to a function sends a copy of the array, not a pointer to it (unless otherwise specified). The size of an array is part of its type. Go does not support the pointer arithmetic tricks that you might use in C. Instead, when you make a pointer to an array, you get a pointer to the actual array, not just a pointer to something that happens to be the first thing in the array.

9  Arrays are one dimensional but can be composed to make multiple dimensions (an array of arrays) [5] [15] int is equivalent to [5] ([15] int)  Syntax: array2D := [2] ([3] int) { {1,2,3}, {4,5,6} } //an array with two elements, each element //being an array with 3 elements

10 Slices  A slice points to an array of values  It also has a built-in length  Syntax: aSlice := []int {1,2,3}  A slice can be cut from an array arrayName := [5] int {1,2,3,4,5} arraySlice := arrayName[a:b]  The size of a slice is b-a  arrayName[a:a] includes no elements  arrayName[a:a+1] includes one element

11  Slices are abstractions of arrays  Slices are more flexible than arrays and as a result are more common in Go code  Slices have no specified lengths like arrays do  Slices can be grown with the built-in copy or append function  The keyword range allows you to loop through an entire slice or array

12  Slices are also created with make  Syntax: arraySlice := make([]int, 5)  This allocates an array of length 5 and returns a slice that refers to the array  All the values are initialized to 0  The make function can also be passed a third parameter to specify the capacity of the slice arraySlice := make([]int, 5, 10) Here, the length is 5, but the slice can grow up to 10

13 Tying it all together  An application: creating a simple address book  addressBook.go maps values in a slice of strings to values in a slice of ints  addressBook2.go uses structs to achieve something similar


Download ppt "Arrays, Slices, and more. Structs  A struct is a collection of fields  The keyword type is used to declare the type of the struct  Syntax: type Vertex."

Similar presentations


Ads by Google