SlideServe
Menu
Préférences
© SlideServe · v2026

Go Language Data Structures

lowri avatar
13 diapositives · 297 Views · Jul 21, 2014

This guide provides an overview of essential data structures in Go, including arrays, slices, maps, and structs. Learn how to declare and initialize structs using the `type` keyword, access fields, and utilize struct literals for convenient initialization. Discover maps for key-value pairing and how to create them using `make`. Additionally, explore arrays and their unique characteristics and differences from C arrays, alongside slices, their flexibility, and dynamic sizing. This resource empowers you to effectively manage data in Go applications.

Go Language Data Structures - slide 1
01 / 13Go Language Data Structures
1 / 17

Télécharger la présentation

Go Language Data Structures
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Arrays, Slices, and more Go Language Data Structures

  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: • varmyMap 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 • varmyMap = 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 • varmyMap = 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