~kotrunga/a-tour-of-go

372aab130de69d94959b75fffc1a1935ca8cb5e7 — kotrunga 9 months ago 552ad88
Ran gofmt
38 files changed, 86 insertions(+), 86 deletions(-)

M basics/flow-control-statements/1.2.10_switch-evaluation-order.go
M basics/flow-control-statements/1.2.11_switch-with-no-condition.go
M basics/flow-control-statements/1.2.12_defer.go
M basics/flow-control-statements/1.2.13_defer-multi.go
M basics/flow-control-statements/1.2.1_for.go
M basics/flow-control-statements/1.2.2_for-continued.go
M basics/flow-control-statements/1.2.3_for-is-gos-while.go
M basics/flow-control-statements/1.2.4_forever.go
M basics/flow-control-statements/1.2.5_if.go
M basics/flow-control-statements/1.2.6_if-with-a-short-statement.go
M basics/flow-control-statements/1.2.7_if-and-else.go
M basics/flow-control-statements/1.2.8_exercise-loops-and-functions.go
M basics/flow-control-statements/1.2.9_switch.go
M basics/structs-slices-maps/1.3.10_slice-bounds.go
M basics/structs-slices-maps/1.3.11_slice-len-cap.go
M basics/structs-slices-maps/1.3.12_nil-slices.go
M basics/structs-slices-maps/1.3.13_making-slices.go
M basics/structs-slices-maps/1.3.14_slices-of-slice.go
M basics/structs-slices-maps/1.3.15_append.go
M basics/structs-slices-maps/1.3.16_range.go
M basics/structs-slices-maps/1.3.17_range-continued.go
M basics/structs-slices-maps/1.3.18_exercise-slices.go
M basics/structs-slices-maps/1.3.19_maps.go
M basics/structs-slices-maps/1.3.1_pointers.go
M basics/structs-slices-maps/1.3.20_map-literals.go
M basics/structs-slices-maps/1.3.21_map-literals-continued.go
M basics/structs-slices-maps/1.3.22_mutating-maps.go
M basics/structs-slices-maps/1.3.23_exercise-maps.go
M basics/structs-slices-maps/1.3.24_function-values.go
M basics/structs-slices-maps/1.3.25_function-closures.go
M basics/structs-slices-maps/1.3.26_exercise-fibonacci-closure.go
M basics/structs-slices-maps/1.3.2_structs.go
M basics/structs-slices-maps/1.3.3_struct-fields.go
M basics/structs-slices-maps/1.3.4_struct-pointers.go
M basics/structs-slices-maps/1.3.5_struct-literals.go
M basics/structs-slices-maps/1.3.7_slices.go
M basics/structs-slices-maps/1.3.8_slices-pointers.go
M basics/structs-slices-maps/1.3.9_slice-literals.go
M basics/flow-control-statements/1.2.10_switch-evaluation-order.go => basics/flow-control-statements/1.2.10_switch-evaluation-order.go +1 -1
@@ 25,4 25,4 @@ func main() {
	default:
		fmt.Println("Too far away.")
	}
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.11_switch-with-no-condition.go => basics/flow-control-statements/1.2.11_switch-with-no-condition.go +1 -1
@@ 21,4 21,4 @@ func main() {
	default:
		fmt.Println("Good evening.")
	}
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.12_defer.go => basics/flow-control-statements/1.2.12_defer.go +1 -1
@@ 13,4 13,4 @@ func main() {
	defer fmt.Println("world!")

	fmt.Println("Hello ")
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.13_defer-multi.go => basics/flow-control-statements/1.2.13_defer-multi.go +1 -1
@@ 18,4 18,4 @@ func main() {
	}

	fmt.Println("done")
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.1_for.go => basics/flow-control-statements/1.2.1_for.go +1 -1
@@ 20,4 20,4 @@ func main() {
		sum += i
	}
	fmt.Println(sum)
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.2_for-continued.go => basics/flow-control-statements/1.2.2_for-continued.go +2 -2
@@ 10,8 10,8 @@ import "fmt"

func main() {
	sum := 1
	for ; sum < 1000; {
	for sum < 1000 {
		sum += sum
	}
	fmt.Println(sum)
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.3_for-is-gos-while.go => basics/flow-control-statements/1.2.3_for-is-gos-while.go +1 -1
@@ 15,4 15,4 @@ func main() {
		sum += sum
	}
	fmt.Println(sum)
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.4_forever.go => basics/flow-control-statements/1.2.4_forever.go +2 -2
@@ 8,6 8,6 @@ package main

func main() {
	for {
		

	}
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.5_if.go => basics/flow-control-statements/1.2.5_if.go +1 -1
@@ 20,4 20,4 @@ func sqrt(x float64) string {

func main() {
	fmt.Println(sqrt(2), sqrt(-4))
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.6_if-with-a-short-statement.go => basics/flow-control-statements/1.2.6_if-with-a-short-statement.go +2 -2
@@ 13,7 13,7 @@ import (
)

func pow(x, n, lim float64) float64 {
	if v:= math.Pow(x, n); v < lim {
	if v := math.Pow(x, n); v < lim {
		return v
	}
	return lim


@@ 24,4 24,4 @@ func main() {
		pow(3, 2, 10),
		pow(3, 3, 20),
	)
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.7_if-and-else.go => basics/flow-control-statements/1.2.7_if-and-else.go +2 -2
@@ 12,7 12,7 @@ import (
)

func pow(x, n, lim float64) float64 {
	if v:= math.Pow(x, n); v < lim {
	if v := math.Pow(x, n); v < lim {
		return v
	} else {
		fmt.Printf("%g >= %g\n", v, lim)


@@ 26,4 26,4 @@ func main() {
		pow(3, 2, 10),
		pow(3, 3, 20),
	)
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.8_exercise-loops-and-functions.go => basics/flow-control-statements/1.2.8_exercise-loops-and-functions.go +3 -3
@@ 28,9 28,9 @@ func Sqrt(x float64) float64 {
func SqrtAccurate(x float64) float64 {
	var z float64 = 1
	var z_previous float64 = 0
	var threshold float64 = 0.0000000001	// 1E-10
	var threshold float64 = 0.0000000001 // 1E-10

	for math.Abs(z - z_previous) >= threshold {
	for math.Abs(z-z_previous) >= threshold {
		//fmt.Println(z)	// uncomment to see z become more precise
		z_previous = z
		z -= (z*z - x) / (2 * z)


@@ 48,4 48,4 @@ func main() {

	fmt.Printf("Sqrt(%d) = %g\n", 25, Sqrt(25))
	fmt.Printf("SqrtAccurate(%d) = %g\n\n", 25, SqrtAccurate(25))
}
\ No newline at end of file
}

M basics/flow-control-statements/1.2.9_switch.go => basics/flow-control-statements/1.2.9_switch.go +1 -1
@@ 24,4 24,4 @@ func main() {
		// plan9, windows...
		fmt.Println("%s.", os)
	}
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.10_slice-bounds.go => basics/structs-slices-maps/1.3.10_slice-bounds.go +4 -4
@@ 11,12 11,12 @@ import "fmt"
func main() {
	s := []int{2, 3, 5, 7, 11, 13}

	s = s[1:4]	// 3, 5, 7
	s = s[1:4] // 3, 5, 7
	fmt.Println(s)

	s = s[:2]	// 3, 5
	s = s[:2] // 3, 5
	fmt.Println(s)

	s = s[1:]	// 5
	s = s[1:] // 5
	fmt.Println(s)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.11_slice-len-cap.go => basics/structs-slices-maps/1.3.11_slice-len-cap.go +1 -1
@@ 31,4 31,4 @@ func main() {

func printSlices(s []int) {
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.12_nil-slices.go => basics/structs-slices-maps/1.3.12_nil-slices.go +1 -1
@@ 15,4 15,4 @@ func main() {
	if s == nil {
		fmt.Println("nil!")
	}
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.13_making-slices.go => basics/structs-slices-maps/1.3.13_making-slices.go +1 -1
@@ 26,4 26,4 @@ func main() {

func printSlice(s string, x []int) {
	fmt.Printf("%s len=%d cap=%d %v\n", s, len(x), cap(x), x)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.14_slices-of-slice.go => basics/structs-slices-maps/1.3.14_slices-of-slice.go +4 -4
@@ 14,9 14,9 @@ import (
func main() {
	// Create a tic-tac-toe board
	board := [][]string{
		[]string{"_","_","_"},
		[]string{"_","_","_"},
		[]string{"_","_","_"},
		[]string{"_", "_", "_"},
		[]string{"_", "_", "_"},
		[]string{"_", "_", "_"},
	}

	// The players take turns


@@ 30,4 30,4 @@ func main() {
	for i := 0; i < len(board); i++ {
		fmt.Printf("%s\n", strings.Join(board[i], " "))
	}
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.15_append.go => basics/structs-slices-maps/1.3.15_append.go +1 -1
@@ 29,4 29,4 @@ func main() {

func printSlice(s []int) {
	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.16_range.go => basics/structs-slices-maps/1.3.16_range.go +1 -1
@@ 17,4 17,4 @@ func main() {
	for i, v := range pow {
		fmt.Printf("2**%d = %d\n", i, v)
	}
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.17_range-continued.go => basics/structs-slices-maps/1.3.17_range-continued.go +1 -1
@@ 23,4 23,4 @@ func main() {
	for _, value := range pow {
		fmt.Printf("%d\n", value)
	}
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.18_exercise-slices.go => basics/structs-slices-maps/1.3.18_exercise-slices.go +3 -3
@@ 21,13 21,13 @@ func Pic(dx, dy int) [][]uint8 {
	// make the slice of uint8 slices
	// of length dy
	result := make([][]uint8, dy)
	

	// range through result, and set each 1st dimension
	// value to be a uint8 slice
	for i := range result {
		result[i] = make([]uint8, dx)
	}
	

	// range through each value of result, and set
	// the value based on where we are in the range
	for i := range result {


@@ 35,7 35,7 @@ func Pic(dx, dy int) [][]uint8 {
			result[i][j] = uint8(0) + uint8(j)
		}
	}
	

	return result
}


M basics/structs-slices-maps/1.3.19_maps.go => basics/structs-slices-maps/1.3.19_maps.go +2 -2
@@ 18,8 18,8 @@ var m map[string]Vertex

func main() {
	m = make(map[string]Vertex)
	m["Bell Labs"] = Vertex {
	m["Bell Labs"] = Vertex{
		40.68433, -74.39967,
	}
	fmt.Println(m["Bell Labs"])
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.1_pointers.go => basics/structs-slices-maps/1.3.1_pointers.go +8 -8
@@ 15,12 15,12 @@ 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 := &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
	p = &j         // point p to j
	*p = *p / 37   // divide j through the pointer
	fmt.Println(j) // see the new value of j
}

M basics/structs-slices-maps/1.3.20_map-literals.go => basics/structs-slices-maps/1.3.20_map-literals.go +4 -4
@@ 12,15 12,15 @@ type Vertex struct {
	Lat, Long float64
}

var m = map[string]Vertex {
	"Bell Labs": Vertex {
var m = map[string]Vertex{
	"Bell Labs": Vertex{
		40.68433, -74.39967,
	},
	"Google": Vertex {
	"Google": Vertex{
		37.42202, -122.08408,
	},
}

func main() {
	fmt.Println(m)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.21_map-literals-continued.go => basics/structs-slices-maps/1.3.21_map-literals-continued.go +4 -4
@@ 13,11 13,11 @@ type Vertex struct {
	Lat, Long float64
}

var m = map[string]Vertex {
	"Bell Labs": { 40.68433, -74.39967 },
	"Google": Vertex { 37.42202, -122.08408 },
var m = map[string]Vertex{
	"Bell Labs": {40.68433, -74.39967},
	"Google":    Vertex{37.42202, -122.08408},
}

func main() {
	fmt.Println(m)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.22_mutating-maps.go => basics/structs-slices-maps/1.3.22_mutating-maps.go +1 -1
@@ 29,4 29,4 @@ func main() {

	v, ok := m["Answer"]
	fmt.Println("The value:", v, "Present?", ok)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.23_exercise-maps.go => basics/structs-slices-maps/1.3.23_exercise-maps.go +7 -7
@@ 17,12 17,12 @@ import (
)

func WordCount(s string) map[string]int {
	words := strings.Fields(s)			// splits the string s into a string array, splitting by spaces, testing with 'unicode.IsSpace'
	wordCount := make(map[string]int)	// expanded: var wordCount map[string]int = make(map[string]int)
	
	for _, word := range words {		// range through each word in 'words'
		_, exists := wordCount[word]	// wordCount is the map (the keys are strings, values are ints), get if the word exists in the map
		
	words := strings.Fields(s)        // splits the string s into a string array, splitting by spaces, testing with 'unicode.IsSpace'
	wordCount := make(map[string]int) // expanded: var wordCount map[string]int = make(map[string]int)

	for _, word := range words { // range through each word in 'words'
		_, exists := wordCount[word] // wordCount is the map (the keys are strings, values are ints), get if the word exists in the map

		if exists {
			wordCount[word] = wordCount[word] + 1
		} else {


@@ 35,4 35,4 @@ func WordCount(s string) map[string]int {

func main() {
	wc.Test(WordCount)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.24_function-values.go => basics/structs-slices-maps/1.3.24_function-values.go +2 -2
@@ 13,7 13,7 @@ import (
)

func compute(fn func(float64, float64) float64) float64 {
	return fn(3,4)
	return fn(3, 4)
}

func main() {


@@ 24,4 24,4 @@ func main() {

	fmt.Println(compute(hypot))
	fmt.Println(compute(math.Pow))
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.25_function-closures.go => basics/structs-slices-maps/1.3.25_function-closures.go +2 -2
@@ 26,7 26,7 @@ func main() {
	for i := 0; i < 10; i++ {
		fmt.Println(
			pos(i),
			neg(-2 * i),
			neg(-2*i),
		)
	}
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.26_exercise-fibonacci-closure.go => basics/structs-slices-maps/1.3.26_exercise-fibonacci-closure.go +6 -6
@@ 23,13 23,13 @@ func fibonacciWithSlices() func() int {
		switch len(nums) {
		case 0:
			nums = append(nums, 0)
			toReturn =  nums[0]
			toReturn = nums[0]
		case 1:
			nums = append(nums, 1)
			toReturn =  nums[1]
			toReturn = nums[1]
		default:
			nums = append(nums, nums[len(nums) - 2] + nums[len(nums) - 1])
			toReturn = nums[len(nums) - 1]
			nums = append(nums, nums[len(nums)-2]+nums[len(nums)-1])
			toReturn = nums[len(nums)-1]
		}

		return toReturn


@@ 41,7 41,7 @@ func fibonacciSimple() func() int {
	a := 0
	b := 1
	c := 0
	

	return func() int {
		if n == 0 {
			n++


@@ 63,4 63,4 @@ func main() {
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.2_structs.go => basics/structs-slices-maps/1.3.2_structs.go +1 -1
@@ 15,4 15,4 @@ type Vertex struct {

func main() {
	fmt.Println(Vertex{1, 2})
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.3_struct-fields.go => basics/structs-slices-maps/1.3.3_struct-fields.go +1 -1
@@ 17,4 17,4 @@ func main() {
	v := Vertex{1, 2}
	v.X = 4
	fmt.Println(v.X)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.4_struct-pointers.go => basics/structs-slices-maps/1.3.4_struct-pointers.go +1 -1
@@ 21,4 21,4 @@ func main() {
	p := &v
	p.X = 1e9
	fmt.Println(v)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.5_struct-literals.go => basics/structs-slices-maps/1.3.5_struct-literals.go +5 -5
@@ 13,12 13,12 @@ type Vertex struct {
}

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
	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
}

M basics/structs-slices-maps/1.3.7_slices.go => basics/structs-slices-maps/1.3.7_slices.go +4 -4
@@ 6,7 6,7 @@
	- Specify a slice like this: a[low : high]
		It excludes the end (high)
	- The difference between arrays and slices when declared are subtle;
	  if there is a size specified, it's an array. 
	  if there is a size specified, it's an array.
*/

package main


@@ 14,8 14,8 @@ package main
import "fmt"

func main() {
	primes := [6]int{2, 3, 5, 7 , 11, 13}	// This is an array
	primes := [6]int{2, 3, 5, 7, 11, 13} // This is an array

	var s []int = primes[1:4]	// This is a slice. Selects [3, 5, 7] from primes.
	var s []int = primes[1:4] // This is a slice. Selects [3, 5, 7] from primes.
	fmt.Println(s)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.8_slices-pointers.go => basics/structs-slices-maps/1.3.8_slices-pointers.go +1 -1
@@ 25,4 25,4 @@ func main() {
	b[0] = "XXX"
	fmt.Println(a, b)
	fmt.Println(names)
}
\ No newline at end of file
}

M basics/structs-slices-maps/1.3.9_slice-literals.go => basics/structs-slices-maps/1.3.9_slice-literals.go +1 -1
@@ 30,4 30,4 @@ func main() {
		{13, true},
	}
	fmt.Println(s)
}
\ No newline at end of file
}