Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Golang Arrays & Slices An array is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length and is never negative The elements can be addressed by integer indices 0 through len(a)-1 Each element in the arrays is implicitly zeroed Arrays are values, ex: var a [3]int where a represent the entire array and not a pointer to the beginning of the arrays. Go provides built-in functions : len, cap, copy on arrays. Golang Arrays & Slices var a [0]int //array of length 0, useless var a [3]int //array of length 3 var a [3]int = [3]int{1,2,3} or a := [3]int{1,2,3} //array of length 3 a = new([]int, 10) //creates an array of 10 ints var a [3]int a = [3]int{1,2,3} a = […]int{1,2,3} // creates array of length 3 Golang Arrays & Slices Arrays are fixed length, slices are not slice is reference to a section of array a[1:3], a[:3], a[3:], a[:] var a []int a = []int{1,2,3} // slice is of length 3 var a [10]int = []int{1,2,3,4,5,6,7,8,9,0} b = a[3] => {4} b = a[3:3] => {} b = a[3:5] => {4,5} b = a[3:] => {4,5,6,7,8,9,0} b = a[:3] => {1,2,3} Golang Arrays & Slices Append – builtin function to append element to an array a := []int{1,2} //a has two elements 1,2 a = append(a, 3) //a has three elements 1,2,3 s = make([]int, 3, 5) s = append(s,4) s = append(s,5) s = append(s,6) // creates a array of length 3 {0,0,0} // adds a new element 4 {0,0,0,4} // adds a new element 4 {0,0,0,4} // adds a new element 4 {0,0,0,4, 5} and capacity increased from 5 to a larger number. Copy s := {1,2,3} var b [5]int copy (s,b) //copy only copies if b has space Golang Arrays Exercise: create a array with first 10 even numbers create a slice from 3rd to 7th element insert element with value 9 between values 8 and 10 and print the array slice.(hint: use copy) {8, 9, 10,12} Extra: create a append function that detects if we run out of capacity and adds an element to the array. Homework: delete the element you have inserted above. Golang Maps A map is a key-value pair Key and Value are two different valid go types Size of map is not fixed like an array Map is like slice, it is a reference type. var m map[string]float64 m = map[string]float64{"1":1, "pi":3.1415} m = make(map[string]float64) Golang Maps Assigning key-value pair m[“1”] = 2 a := m[“1”] Delete an entry from the map with specified key. delete(m,“1”) // deletes and entry builtins useful are len, copy, make Golang Maps Check existense of a key in the map value, ok := m[x] Is ok true/false? Golang Maps Exercise: Create a map with the following association Player from a base ball team A Number of points in the season Joe 22 Alex 48 Gary 12 Frank 32 Mike 24 Print the map. Write code to see if Player Mike exists delete player Mike as he left the team A check to see if Mike is still associated to team A Docker Dojo - golang Complex Arrays and Maps Two Dimensional Arrays can describe associations var a [][]string Maps can be a map of var a map[string]map[string]int Map can also have another complex data structure as value var a map[string][]int … Golang Structures Struct in Go represents an object. But unlike any object oriented language, structs in Go do not compose methods Contains named fields Structs can have associated methods type point struct { x, y float64 } Structs are values Structs and their fields can be exported type Point struct { x, Y float64 } Golang Structures Struct initialization Type Point struct { x, y float64 } var p Point p = Point{} p = Point{2.2,3.3} p = Point{y:3.3} pp = new (Point) (*pp).x = 1.1, pp.y = 2.2 Golang Structures Exercise create a structure rectangle with length and width create a structure for circle with radius and origin create a sample instance of both and print their description. ex: myrectangle has following dimensions, length = 2.0 abd width = 3.0 mycircle has origin at (1, 3.2) and a radius of 3.15 Golang Structures Anonymous fields: type anotherStruct { x,y int } type point struct { anotherStruct x, y float64 } p := point { {1,2}, 1.0, 2.0} fmt.Println(p.anotherStruct)