development-ib-fork/web/07/demos/05_encode_json.go

29 lines
507 B
Go

package main
import (
"encoding/json"
"net/http"
)
type jsonEncodeHandler int
type User struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Age int `json:"age"`
}
func (jsonHandler jsonEncodeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mariam := User{
Firstname: "Mariam",
Lastname: "Okonkwo",
Age: 25,
}
json.NewEncoder(w).Encode(mariam)
}
func main() {
var enc jsonEncodeHandler
http.ListenAndServe("localhost:8080", enc)
}