package main
import (
"fmt"
"net/http"
)
type DB struct{
name string
age int
}
var gdb DB = DB{"catty",26}
func get(w http.ResponseWriter, req *http.Request) {
// The "/" pattern matches everything, so we need to check
// that we're at the root here.
if req.URL.Path != "/get" {
http.NotFound(w, req)
return
}
fmt.Fprintf(w, "Welcome to the home page!\n")
fmt.Fprintf(w, "name is %s, age is %d\n",gdb.name,gdb.age)
}
func (db *DB)list(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/list" {
http.NotFound(w, req)
return
}
fmt.Fprintf(w, "Welcome to the home page!\n")
fmt.Fprintf(w, "name is %s, age is %d\n",db.name,db.age)
w.Write([]byte("json message"))
}
func main(){
db:=DB{"doggy",27}
mux := http.NewServeMux()
mux.HandleFunc("/get",get)
mux.HandleFunc("/list",db.list)
http.ListenAndServe("localhost:1234",mux)
}