10dd31bf9cab0052c47987ee5900d32cb689277b — kotrunga 8 months ago
Added sections 1.3.1 through 1.3.10 inclusive
A basics/structs-slices-maps/1.3.10_slice-bounds.go => basics/structs-slices-maps/1.3.10_slice-bounds.go +22 -0
@@ 0,0 1,22 @@
+ /*
+ 	Notes 1.3.10
+ 	- You can omit the low/high bound from a slice to use the defaults
+ */
+ package main
+ import "fmt"
+ func main() {
+ 	s := []int{2, 3, 5, 7, 11, 13}
+ 	s = s[1:4]	// 3, 5, 7
+ 	fmt.Println(s)
+ 	s = s[:2]	// 2, 3
+ 	fmt.Println(s)
+ 	s = s[1:]	// 3, 5, 7, 11, 13
+ 	fmt.Println(s)
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.1_pointers.go => basics/structs-slices-maps/1.3.1_pointers.go +26 -0
@@ 0,0 1,26 @@
+ /*
+ 	Notes 1.3.1
+ 	- Pointers hold the memory address of a value
+ 	- Zero value is nil
+ 	- The & operator gives the address of the variable
+ 	- * dereferences the pointer
+ 	- Go has no pointer arithmetic
+ */
+ package main
+ import "fmt"
+ func main() {
+ 	i, j := 42, 2701
+ 	p := &i	// point to i
+ 	fmt.Println(*p)	// read i through the pointer p
+ 	*p = 21	// set i through pointer p
+ 	fmt.Println(i)	// see the new value of i
+ 	p = &j	// point p to j
+ 	*p = *p / 37	// divide j through the pointer
+ 	fmt.Println(j)	// see the new value of j
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.2_structs.go => basics/structs-slices-maps/1.3.2_structs.go +18 -0
@@ 0,0 1,18 @@
+ /*
+ 	Notes 1.3.2
+ 	- A struct is a collection of fields
+ */
+ package main
+ import "fmt"
+ type Vertex struct {
+ 	X int
+ 	Y int
+ }
+ func main() {
+ 	fmt.Println(Vertex{1, 2})
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.3_struct-fields.go => basics/structs-slices-maps/1.3.3_struct-fields.go +20 -0
@@ 0,0 1,20 @@
+ /*
+ 	Notes 1.3.3
+ 	- Struct fields are accessed with the . operator
+ */
+ package main
+ import "fmt"
+ type Vertex struct {
+ 	X int
+ 	Y int
+ }
+ func main() {
+ 	v := Vertex{1, 2}
+ 	v.X = 4
+ 	fmt.Println(v.X)
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.4_struct-pointers.go => basics/structs-slices-maps/1.3.4_struct-pointers.go +23 -0
@@ 0,0 1,23 @@
+ /*
+ 	Notes 1.3.4
+ 	- You can make struct pointers
+ 	- We could write (*p).X to access the X field in the 'v' Vertex struct
+ 	  Shorthand just uses the dot operator, removing the explicit dereference
+ */
+ package main
+ import "fmt"
+ type Vertex struct {
+ 	X int
+ 	Y int
+ }
+ func main() {
+ 	v := Vertex{1, 2}
+ 	p := &v
+ 	p.X = 1e9
+ 	fmt.Println(v)
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.5_struct-literals.go => basics/structs-slices-maps/1.3.5_struct-literals.go +24 -0
@@ 0,0 1,24 @@
+ /*
+ 	Notes 1.3.5
+ 	- Building structs!
+ */
+ package main
+ import "fmt"
+ type Vertex struct {
+ 	X, Y int
+ }
+ var (
+ 	v1 = Vertex{1, 2}	// has type Vertex
+ 	v2 = Vertex{X: 1}	// Y:0 is implicit
+ 	v3 = Vertex{}			// X:0 and Y:0
+ 	p = &Vertex{1, 2}	// has type *Vertex
+ )
+ func main() {
+ 	fmt.Println(v1, p, v2, v3)
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.6_array.go => basics/structs-slices-maps/1.3.6_array.go +23 -0
@@ 0,0 1,23 @@
+ /*
+ 	Notes 1.3.6
+ 	- In Go, an array's length is part of it's type
+ */
+ package main
+ import "fmt"
+ func main() {
+ 	var astring
+ 	a = "Hello"
+ 	a = "World"
+ 	fmt.Println(a,a)
+ 	fmt.Println(a)
+ 	var a := 6
+ 	fmt.Println(a)
+ 	primes := int{2, 3, 5, 7, 11, 13}
+ 	fmt.Println(primes)
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.7_slices.go => basics/structs-slices-maps/1.3.7_slices.go +19 -0
@@ 0,0 1,19 @@
+ /*
+ 	Notes 1.3.6
+ 	- A slice is a dynamically-sized, flexible view into the elements of an array
+ 	- Slices are more common than arrays in practice
+ 	- Specify a slice like this: a[low : high]
+ 		It excludes the end (high)
+ */
+ package main
+ import "fmt"
+ func main() {
+ 	primes := int{2, 3, 5, 7 , 11, 13}
+ 	var s []int = primes[1:4]	// selects [3, 5, 7] from primes
+ 	fmt.Println(s)
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.8_slices-pointers.go => basics/structs-slices-maps/1.3.8_slices-pointers.go +28 -0
@@ 0,0 1,28 @@
+ /*
+ 	Notes 1.3.8
+ 	- A slice doesn't story any data; it's just an abstraction over an array
+ 	- Changing the elements of a slice change the actual array
+ */
+ package main
+ import "fmt"
+ func main() {
+ 	names := string{
+ 		"John",
+ 		"Paul",
+ 		"George",
+ 		"Ringo",
+ 	}
+ 	fmt.Println(names)
+ 	a := names[0:2]
+ 	b := names[1:3]
+ 	fmt.Println(a, b)
+ 	b = "XXX"
+ 	fmt.Println(a, b)
+ 	fmt.Println(names)
+ }<
\ No newline at end of file

A basics/structs-slices-maps/1.3.9_slice-literals.go => basics/structs-slices-maps/1.3.9_slice-literals.go +33 -0
@@ 0,0 1,33 @@
+ /*
+ 	Notes 1.3.9
+ 	- A slice literal is like an array literal... without the length
+ 	- Array literal: bool{true, true, false}
+ 	- Slice literal: []bool{true, true, false}
+ 	  This will build an array, and then a slice that references it
+ */
+ package main
+ import "fmt"
+ func main() {
+ 	q := []int{2, 3, 5, 7, 11, 13}
+ 	fmt.Println(q)
+ 	r := []bool{true, false, true, true, false, true}
+ 	fmt.Println(r)
+ 	s := []struct {
+ 		i int
+ 		b bool
+ 	}{
+ 		{2, true},
+ 		{3, false},
+ 		{5, true},
+ 		{7, true},
+ 		{11, false},
+ 		{13, true},
+ 	}
+ 	fmt.Println(s)
+ }<
\ No newline at end of file