Today, the weather was less than attractive so I thought I’d play around with Go routines, channels, and functions.
Take a look at the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import "fmt"
type fTyp struct {
Doer func ( int ) error
Seeker func ( int , func ( int ) error ) error
Wander func ( int , func ( int , func ( int ) error ) error ) error
With int
}
func main () {
var do = make ( chan fTyp )
var o fTyp = fTyp { Doer : foo , Seeker : bar , Wander : wander , With : 3 }
go func ( f fTyp ) {
do <- f
}( o )
stuff := <- do
stuff . Doer ( stuff . With )
stuff . Seeker ( stuff . With , stuff . Doer )
stuff . Wander ( stuff . With , stuff . Seeker )
}
func foo ( id int ) error {
fmt . Printf ( "foo: %d\n" , id )
return nil
}
func bar ( id int , f func ( int ) error ) error {
f ( id )
return nil
}
func wander ( id int , f func ( int , func ( int ) error ) error ) error {
f ( id , foo )
return nil
}
Who’s ready to go (no pun intended) a step further and create an additional member to the fTyp struct and corresponding functions that will extend the meta-abstraction passing stuff.Wander as a parameter?
The things we do in the name of entertainment!