go to practice: HTTP processing

This article is to link notes when the guide go of it.

package main

import (
	"fmt"
	"log"
	"net/http"
)

type String  string

type Struct struct{
	Greeting string
	Punct string
	Who string
}


func main(){
	http.Handle("/string",String("aaaaaaaaaaaaaaaaaaaa2"))
	http.Handle("/struct", Struct{
		Greeting: "Hello",
		Punct:    "",
		Who:      "gopher",
	})
	log.Fatal(http.ListenAndServe("localhost:4000",nil))
}

func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request){
	fmt.Fprint(w,s)
}
func (s Struct) ServeHTTP(w http.ResponseWriter, r *http.Request){
	fmt.Fprint(w,s)
}

  The above http.handle () inside a url, if repeated, then, will complain

Guess you like

Origin www.cnblogs.com/prader6/p/12076514.html