publish function sequence
This commit is contained in:
parent
91c4866d20
commit
904a742d0c
@ -9,11 +9,11 @@ Arrays
|
|||||||
Slices
|
Slices
|
||||||
Maps
|
Maps
|
||||||
Range
|
Range
|
||||||
# Functions
|
Functions
|
||||||
# Multiple Return Values
|
Multiple Return Values
|
||||||
# Varadic Functions
|
# Varadic Functions
|
||||||
# Closures
|
Closures
|
||||||
# Recursion
|
Recursion
|
||||||
# Defer
|
# Defer
|
||||||
# Panic
|
# Panic
|
||||||
# Pointers
|
# Pointers
|
||||||
|
@ -1,20 +1,40 @@
|
|||||||
|
// Go supports [_anonymous functions_](http://en.wikipedia.org/wiki/Anonymous_function),
|
||||||
|
// which can form <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)"><em>closures</em></a>.
|
||||||
|
// Anonymous functions are useful when you want to define
|
||||||
|
// a function inline without having to name it.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func makeEvenGenerator() func() uint {
|
// This function `intSeq` returns another function, which
|
||||||
i := uint(0)
|
// we define anonymously in the body of `intSeq`. The
|
||||||
return func() uint {
|
// returned function _closes over_ the variable `i` to
|
||||||
i += 2
|
// form a closure.
|
||||||
|
func intSeq() func() int {
|
||||||
|
i := 0
|
||||||
|
return func() int {
|
||||||
|
i += 1
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
nextEven := makeEvenGenerator()
|
|
||||||
fmt.Println(nextEven())
|
|
||||||
fmt.Println(nextEven())
|
|
||||||
fmt.Println(nextEven())
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: note example of first-class functions.
|
// We call `intSeq`, assigning the result (a function)
|
||||||
|
// to `nextInt`. This function value captures its
|
||||||
|
// own `i` value, which will be updated each time
|
||||||
|
// we call `nextInt`.
|
||||||
|
nextInt := intSeq()
|
||||||
|
|
||||||
|
// See the effect of the closure by calling `nextInt`
|
||||||
|
// a few times.
|
||||||
|
fmt.Println(nextInt())
|
||||||
|
fmt.Println(nextInt())
|
||||||
|
fmt.Println(nextInt())
|
||||||
|
|
||||||
|
// To confirm that the state is unique to that
|
||||||
|
// particular function, create and test a new one.
|
||||||
|
newInts := intSeq()
|
||||||
|
fmt.Println(newInts())
|
||||||
|
}
|
||||||
|
8
examples/closures/closures.sh
Normal file
8
examples/closures/closures.sh
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
$ go run closures.go
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
1
|
||||||
|
|
||||||
|
# The last feature of functions we'll look at for now is
|
||||||
|
# recursion.
|
@ -1,34 +1,26 @@
|
|||||||
// Funcations are critical in Go as in any other language.
|
// _Functions_ are central in Go. We'll learn about
|
||||||
// We'll look at some basic examples first.
|
// functions with a few different examples.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
// The syntax is `func name(args) returns { body }`. Note
|
// Here's a function that takes two `int`s and returns
|
||||||
// the the types of arguments come after their name.
|
// their sum as an `int`.
|
||||||
// In this case we're taking a slice of floats and
|
func plus(a int, b int) int {
|
||||||
// returning a single float value.
|
|
||||||
func avg(vals []float64) float64 {
|
// Go requires explicit returns, i.e. it won't
|
||||||
total := 0.0
|
|
||||||
for _, val := range vals {
|
|
||||||
total += val
|
|
||||||
}
|
|
||||||
// Go requires an expliciti return, i.e. it won't
|
|
||||||
// automatically return the value of the last
|
// automatically return the value of the last
|
||||||
// expression.
|
// expression.
|
||||||
return total / float64(len(vals))
|
return a + b
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
input := []float64{98, 93, 77, 82, 83}
|
|
||||||
fmt.Println(input)
|
|
||||||
// Call a function just as you'd expect, with
|
// Call a function just as you'd expect, with
|
||||||
// `name(args)`.
|
// `name(args)`.
|
||||||
output := avg(input)
|
res := plus(1, 2)
|
||||||
fmt.Println(output)
|
fmt.Println("1+2 =", res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// There are several other features to Go functions,
|
// todo: coalesced parameter types
|
||||||
// including multiple return values and varargs, which
|
|
||||||
// we'll look at next.
|
|
||||||
|
5
examples/functions/functions.sh
Normal file
5
examples/functions/functions.sh
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
$ go run functions.go
|
||||||
|
1+2 = 3
|
||||||
|
|
||||||
|
# There are several other features to Go functions. One is
|
||||||
|
# multiple return values, which we'll look at next.
|
@ -1,4 +1,4 @@
|
|||||||
// Go has built-in support for multiple return values.
|
// Go has built-in support for _multiple return values_.
|
||||||
// This feature is used often in idiomatic Go, for example
|
// This feature is used often in idiomatic Go, for example
|
||||||
// to return both result and error values from a function.
|
// to return both result and error values from a function.
|
||||||
|
|
||||||
@ -6,15 +6,16 @@ package main
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
// The `(int, int)` in this signature shows that the
|
// The `(int, int)` in this function signature shows that
|
||||||
// function returns 2 ints.
|
// the function returns 2 `int`s.
|
||||||
func vals() (int, int) {
|
func vals() (int, int) {
|
||||||
return 3, 7
|
return 3, 7
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Here we use the 2 different return values from the
|
// Here we use the 2 different return values from the
|
||||||
// call, i.e. perform multiple assignement.
|
// call with _multiple assignment_.
|
||||||
a, b := vals()
|
a, b := vals()
|
||||||
fmt.Println(a)
|
fmt.Println(a)
|
||||||
fmt.Println(b)
|
fmt.Println(b)
|
||||||
@ -24,3 +25,6 @@ func main() {
|
|||||||
_, c := vals()
|
_, c := vals()
|
||||||
fmt.Println(c)
|
fmt.Println(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo: named return parameters
|
||||||
|
// todo: naked returns
|
||||||
|
@ -2,3 +2,6 @@ $ go run multiple-return-values.go
|
|||||||
3
|
3
|
||||||
7
|
7
|
||||||
7
|
7
|
||||||
|
|
||||||
|
# Another key aspect of functions in Go is their ability
|
||||||
|
# to form closures, which we'll look at next.
|
||||||
|
@ -1,14 +1,20 @@
|
|||||||
|
// Go supports
|
||||||
|
// <a href="http://en.wikipedia.org/wiki/Recursion_(computer_science)"><em>recursive functions</em></a>.
|
||||||
|
// Here's a classic factorial example.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func factorial(x uint) uint {
|
// This `fact` function calls itself until it reaches the
|
||||||
if x == 0 {
|
// base case of `fact(0)`.
|
||||||
|
func fact(n int) int {
|
||||||
|
if n == 0 {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return x * factorial(x-1)
|
return n * fact(n-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(factorial(7))
|
fmt.Println(fact(7))
|
||||||
}
|
}
|
||||||
|
2
examples/recursion/recursion.sh
Normal file
2
examples/recursion/recursion.sh
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
$ go run recursion.go
|
||||||
|
5040
|
Loading…
x
Reference in New Issue
Block a user