HTTP Servers
JSON Response Text
Many handlers return JSON-shaped text with stable field names.
json response
A handler can build a small JSON response body from deterministic values.
JSON Response Text
json_response.go
Replay: real traced execution (multi-file project)
package main
import "fmt"
func main() {
var name = "Ada"
status := "active"
body := "{\"name\":\"" + name + "\",\"status\":\"" + status + "\"}"
headers := map[string]string{
"Content-Type": "application/json",
}
fmt.Println("name=", name)
fmt.Println("contentType=", headers["Content-Type"])
fmt.Println("body=", body)
}
package main
import "fmt"
func main() {
var name = "Linus"
status := "active"
body := "{\"name\":\"" + name + "\",\"status\":\"" + status + "\"}"
headers := map[string]string{
"Content-Type": "application/json",
}
fmt.Println("name=", name)
fmt.Println("contentType=", headers["Content-Type"])
fmt.Println("body=", body)
}
package main
import "fmt"
func main() {
var name = "Grace"
status := "active"
body := "{\"name\":\"" + name + "\",\"status\":\"" + status + "\"}"
headers := map[string]string{
"Content-Type": "application/json",
}
fmt.Println("name=", name)
fmt.Println("contentType=", headers["Content-Type"])
fmt.Println("body=", body)
}
name ← "Ada", status ← "active", body ← "{\"name\":\"Ada\",\"status\":\"active\"}"
5func main() {6 var name→ "Ada" = "Ada" //@name="Linus", "Grace"7 status→ "active" := "active"8 body→ "{\"name\":\"Ada\",\"status\":\"active\"}" := "{\"name\":\"" + name"Ada" + "\",\"status\":\"" + status"active" + "\"}"9 headers→ map[string]string{"Content-Type":"application/json"} := map[string]string{10 "Content-Type": "application/json",11 }1213 fmt.Println("name=", name"Ada")14 fmt.Println("contentType=", headers["Content-Type"]"application/json")15 fmt.Println("body=", body"{\"name\":\"Ada\",\"status\":\"active\"}")16}outputname= Ada contentType= application/json body= {"name":"Ada","status":"active"}
name ← "Linus", status ← "active", body ← "{\"name\":\"Linus\",\"status\":\"active\"}"
5func main() {6 var name→ "Linus" = "Linus"7 status→ "active" := "active"8 body→ "{\"name\":\"Linus\",\"status\":\"active\"}" := "{\"name\":\"" + name"Linus" + "\",\"status\":\"" + status"active" + "\"}"9 headers→ map[string]string{"Content-Type":"application/json"} := map[string]string{10 "Content-Type": "application/json",11 }1213 fmt.Println("name=", name"Linus")14 fmt.Println("contentType=", headers["Content-Type"]"application/json")15 fmt.Println("body=", body"{\"name\":\"Linus\",\"status\":\"active\"}")16}outputname= Linus contentType= application/json body= {"name":"Linus","status":"active"}
name ← "Grace", status ← "active", body ← "{\"name\":\"Grace\",\"status\":\"active\"}"
5func main() {6 var name→ "Grace" = "Grace"7 status→ "active" := "active"8 body→ "{\"name\":\"Grace\",\"status\":\"active\"}" := "{\"name\":\"" + name"Grace" + "\",\"status\":\"" + status"active" + "\"}"9 headers→ map[string]string{"Content-Type":"application/json"} := map[string]string{10 "Content-Type": "application/json",11 }1213 fmt.Println("name=", name"Grace")14 fmt.Println("contentType=", headers["Content-Type"]"application/json")15 fmt.Println("body=", body"{\"name\":\"Grace\",\"status\":\"active\"}")16}outputname= Grace contentType= application/json body= {"name":"Grace","status":"active"}