This commit is contained in:
Mark McGranaghan 2012-09-20 22:15:58 -07:00
parent eb6a9c6b2b
commit b943fcfb74
5 changed files with 41 additions and 57 deletions

View File

@ -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
*/

View File

@ -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
*/

18
021-varadic.go Normal file
View File

@ -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...)`.
}

View File

@ -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...)`.
}

View File

@ -18,7 +18,7 @@ ok-guards
nesting
functions
returns
varargs
varadic
closures
recursion
defer