go-tour: add solution for HTTP exercise
Fixes #176.
LGTM=minux.ma
R=golang-codereviews, minux.ma, mikioh.mikioh
CC=golang-codereviews
https://golang.org/cl/96650043
diff --git a/solutions/http.go b/solutions/http.go
new file mode 100644
index 0000000..9fe3d5f
--- /dev/null
+++ b/solutions/http.go
@@ -0,0 +1,38 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build ignore
+
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+)
+
+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.Fprintf(w, "%s%s %s", s.Greeting, s.Punct, s.Who)
+}
+
+func main() {
+ http.Handle("/string", String("I'm a frayed knot."))
+ http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
+ err := http.ListenAndServe("localhost:4000", nil)
+ if err != nil {
+ log.Fatal(err)
+ }
+}