Skip to content

Instantly share code, notes, and snippets.

@cli248
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cli248/8699283 to your computer and use it in GitHub Desktop.
Save cli248/8699283 to your computer and use it in GitHub Desktop.
A Tour of Go solutions
// #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())
}
}
// #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