23b34f73de089a8af74de83e6caefa79d2db9c64 — kotrunga 6 months ago d13300b
More files on slices, and a range example
M basics/structs-slices-maps/1.3.13_making-slices.go => basics/structs-slices-maps/1.3.13_making-slices.go +1 -1
@@ 4,7 4,7 @@ - You can make slices with the built in make() function
  	- The make() function allocates a zeroed array and returns a slice that points to it
  	- make(type, length, capacity)
- 	*/
+ */
  
  package main
  

A basics/structs-slices-maps/1.3.14_slices-of-slice.go => basics/structs-slices-maps/1.3.14_slices-of-slice.go +33 -0
@@ 0,0 1,33 @@
+ /*
+ 	Notes 1.3.14
+ 
+ 	Slices can hold any type, which includes other slices.
+ */
+ 
+ package main
+ 
+ import (
+ 	"fmt"
+ 	"strings"
+ )
+ 
+ func main() {
+ 	// Create a tic-tac-toe board
+ 	board := [][]string{
+ 		[]string{"_","_","_"},
+ 		[]string{"_","_","_"},
+ 		[]string{"_","_","_"},
+ 	}
+ 
+ 	// The players take turns
+ 	board[0][0] = "X"
+ 	board[2][2] = "X"
+ 	board[1][2] = "X"
+ 	board[1][0] = "X"
+ 	board[0][2] = "X"
+ 
+ 	// Print the board
+ 	for i := 0; i < len(board); i++ {
+ 		fmt.Printf("%s\n", strings.Join(board[i], " "))
+ 	}
+ }< 
\ No newline at end of file

A basics/structs-slices-maps/1.3.15_append.go => basics/structs-slices-maps/1.3.15_append.go +32 -0
@@ 0,0 1,32 @@
+ /*
+ 	Notes 1.3.15
+ 
+ 	Go allows you to append new elements to slices.
+ 
+ 	Use the append() function.
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ func main() {
+ 	var s []int
+ 	printSlice(s)
+ 
+ 	// append works on nil slices.
+ 	s = append(s, 0)
+ 	printSlice(s)
+ 
+ 	// The slice grows as needed.
+ 	s = append(s, 1)
+ 	printSlice(s)
+ 
+ 	// We can add more than one element at a time
+ 	s = append(s, 2, 3, 4)
+ 	printSlice(s)
+ }
+ 
+ func printSlice(s []int) {
+ 	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
+ }< 
\ No newline at end of file

A basics/structs-slices-maps/1.3.16_range.go => basics/structs-slices-maps/1.3.16_range.go +20 -0
@@ 0,0 1,20 @@
+ /*
+ 	Notes 1.3.16
+ 
+ 	'range' is a form of a for loop that iterates over a slice or a map.
+ 
+ 	Two values are always returned each iteration: the index, and the copy of the
+ 	element at that index.
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
+ 
+ func main() {
+ 	for i, v := range pow {
+ 		fmt.Printf("2**%d = %d\n", i, v)
+ 	}
+ }< 
\ No newline at end of file