Data Structures
CIS 193 – Go Programming
Prakhar Bhandari, Adel Qalieh
CIS 193
Prakhar Bhandari, Adel Qalieh
CIS 193
const a = 3 const ( x, y = 1, 2 base, width, height = 3, 3, 4 name = "Test" )
iota starts at 0 and increments
const ( Sunday = iota // Sunday = 0 Monday // Monday = 1 Tuesday // Tuesday = 2 ... Saturday // Saturday = 6 )
Syntax
type name underlying-type
Example
type Mile float64 type Kilometer float64
Why is this useful?
Scope is a compile time property
Not visible outside the lexical block it is declared inside
func main() { x := "cis 193" for i := 0; i < len(x); i++ { x := x[i] if i < 3 { x := x + 'A' - 'a' fmt.Printf("%c\n", x) // "CIS" } } }
s := "Hello world!" fmt.Println(len(s)) // "12"
s := "世界" fmt.Println(len(s)) // "6"
s[i:j]
yields new strings (can also use s[i:]
, s[:i]
or s[:]
)import "unicode/utf8" s := "Hello, 世界" fmt.Println(len(s)) // "13" fmt.Println(utf8.RuneCountInString(s)) // "9"
for i := 0; i < len(s); { r, size := utf8.DecodeRuneInString(s[i:]) fmt.Printf("Rune #%d is %c\n", i, r) i += size }
for i, r := range "Hello, 世界" { fmt.Printf("Index %d has rune: %c\n", i, r) }
What do these do?
Useful verbs
See golang.org/pkg/fmt/ for all printing verbs.
Precision and padding
Fixed length sequence of zero or more elements of the same type
Example
var arr [4]int // array of 4 integers
Array Literals
var arr [3]int = [3]int{1, 2, 4} var arr [3]int = [3]int{1, 2} // arr[2] = 0 arr := [...]int{1, 2, 3, 4} arr := [...]int{10: -1} // array of length 11 with the last index = -1, all else = 0
Not passed by reference, a new copy is made for any function call
Can be compared with == operator
Variable length sequence of the same type
Has three components
Passed by reference - much more frequently used than arrays
Cannot compare with == operator
Slices are pointers to an underlying array
months := [...]string{"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} myFaveMonths := months[4:7] // [April May June] summer := months[6:9] // [June July August]
What is summer[:5]
?
Creating slices
make([]Type, len, cap) make([]Type, len) // cap = len var variableName []Type
Appending to slices
var runes []rune for _, r := range "Hello" { runes := append(runes, r) }
Is there an easier way of doing the above functionality?
a = append(a, 1) a = append(a, 1, 2, 3) a = append(a, a...) // supplies a list of arguments from a slice
map[K]V
Creating maps
ages := make(map[string]int) ages := map[string]int{ "henry": 4, "jim": 5, }
Adding/Deleting elements
ages["bob"] = 32 delete(ages, "jim")
Maps are passed by reference
Maps lookups where the key isn't found returns the zero value
ages["alice"] = ages["alice"] + 1 // equivalent to ages["alice"]++
Iterating over maps
for key, value := range map {...}
How do we check if something is in a map?
age, ok := ages["steve"] // ok is a boolean if age, ok := ages["steve"]; !ok {...} // do something if "steve" not in ages
Can use maps as sets
Aggregate data type that groups zero or more named values of arbitrary types
type Person struct { ID int Name string Age int } var jim Person jim.Age = 34
Field order matters
Fields are only exported if uppercase first letter
type Point struct { X, Y int } p1 := Point{1, 2} p2 := Point{X: 2} // Y = 0
If each field is comparable, structs can be compared using the == operator
Function syntax
func name(parameter-list) (result-list) { body }
None, one, or multiple return types
Result list can have variable names - why?
func Split(path String) (dir, file string) {...}