Source file tour/solutions/http.go

     1  // Copyright 2014 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build ignore
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"log"
    12  	"net/http"
    13  )
    14  
    15  type String string
    16  
    17  func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    18  	fmt.Fprint(w, s)
    19  }
    20  
    21  type Struct struct {
    22  	Greeting string
    23  	Punct    string
    24  	Who      string
    25  }
    26  
    27  func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    28  	fmt.Fprintf(w, "%s%s %s", s.Greeting, s.Punct, s.Who)
    29  }
    30  
    31  func main() {
    32  	http.Handle("/string", String("I'm a frayed knot."))
    33  	http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
    34  	err := http.ListenAndServe("localhost:4000", nil)
    35  	if err != nil {
    36  		log.Fatal(err)
    37  	}
    38  }
    39  

View as plain text