Last active
August 29, 2015 13:55
-
-
Save cli248/8699283 to your computer and use it in GitHub Desktop.
A Tour of Go solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #46 Fibonacci closure | |
package main | |
import "fmt" | |
func fibonacci() func() int { | |
x, y := 0, 1 | |
return func() int { | |
x, y = y, x+y | |
return x | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #60 HTTP Handlers | |
package main | |
import ( | |
"net/http" | |
"fmt" | |
) | |
type String string | |
func (s String) ServeHTTP( | |
w http.ResponseWriter, | |
r *http.Request) { | |
fmt.Fprint(w, s) | |
} | |
type Struct struct { | |
Greeting string | |
Punct string | |
Who string | |
} | |
func (s Struct) ServeHTTP( | |
w http.ResponseWriter, | |
r *http.Request) { | |
fmt.Fprint(w, s.Greeting, s.Punct, s.Who) | |
} | |
func main() { | |
http.ListenAndServe("localhost:4000", nil) | |
http.Handle("/string", String("I'm a frayed know.")) | |
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment