diff --git a/002-values.go b/002-values.go index c12f0ab..3bba1f7 100644 --- a/002-values.go +++ b/002-values.go @@ -1,13 +1,26 @@ -package main - +package main // Go has various value types, including strings, + // different types of numbers, booleans, etc. import "fmt" -func zero(x int) { - x = 0 +func main() { + fmt.Println("Hello world") // Strings. + fmt.Println("Hello " + "other") + + fmt.Println("1+1 =", 1+1) // Integers and floats. + fmt.Println("7.0/3.0 =", 7.0/3.0) + + fmt.Println(true && false) // Booleans. + fmt.Println(true || false) + fmt.Println(!true) } -func main() { - x := 5 - zero(x) - fmt.Println(x) -} +/* +$ go run values.go +Hello world +Hello other +1+1 = 2 +7.0/3.0 = 2.3333333333333335 +false +true +false +*/ diff --git a/02-values.go b/02-values.go deleted file mode 100644 index 3bba1f7..0000000 --- a/02-values.go +++ /dev/null @@ -1,26 +0,0 @@ -package main // Go has various value types, including strings, - // different types of numbers, booleans, etc. -import "fmt" - -func main() { - fmt.Println("Hello world") // Strings. - fmt.Println("Hello " + "other") - - fmt.Println("1+1 =", 1+1) // Integers and floats. - fmt.Println("7.0/3.0 =", 7.0/3.0) - - fmt.Println(true && false) // Booleans. - fmt.Println(true || false) - fmt.Println(!true) -} - -/* -$ go run values.go -Hello world -Hello other -1+1 = 2 -7.0/3.0 = 2.3333333333333335 -false -true -false -*/ diff --git a/021-varadic.go b/021-varadic.go new file mode 100644 index 0000000..f764108 --- /dev/null +++ b/021-varadic.go @@ -0,0 +1,18 @@ +package main // Varadic functions can be called with any number of + // trailing arguments. +import "fmt" + +func add(nums ...int) int { // Varadic args are declared with `...type` and passed in as + for _, num := range nums { // a slice. + total += num + } + return total +} + +func main() { + fmt.Println(add(1, 2)) // Varadic functions can be called in the usual way. + fmt.Println(add(1, 2, 3, 4)) + + nums := []int{2, 3, 4, 5} // If you already have multiple args in a `slice`, apply + fmt.Println1(add(1, nums...)) // them to a varadic function using `func(arg, slice...)`. +} diff --git a/021-varargs.go b/021-varargs.go deleted file mode 100644 index 42a4434..0000000 --- a/021-varargs.go +++ /dev/null @@ -1,21 +0,0 @@ - // Varadic functions can be called with any number of - // trailing arguments. - -package main - -import "fmt" - -func add(nums ...int) int { // Varadic args are declared with `...type` and passed in as - for _, num := range nums { // a slice. - total += num - } - return total -} - -func main() { - fmt.Println(add(1, 2)) // Varadic functions can be called in the usual way. - fmt.Println(add(1, 2, 3, 4)) - - nums := []int{2, 3, 4, 5} // If you already have multiple args in a `slice`, apply - fmt.Println1(add(1, nums...)) // them to a varadic function using `func(arg, slice...)`. -} diff --git a/tool/index.txt b/tool/index.txt index 0c788a3..9d90a66 100644 --- a/tool/index.txt +++ b/tool/index.txt @@ -18,7 +18,7 @@ ok-guards nesting functions returns -varargs +varadic closures recursion defer