Skip to content

Commit 25b6c0c

Browse files
committed
add json.go
1 parent 88939cf commit 25b6c0c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

json.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
type Greeting struct {
10+
Message string `json:"msg"`
11+
}
12+
13+
func hello(w http.ResponseWriter, r *http.Request) {
14+
person := Greeting{Message: "Hello"}
15+
jsonPerson, err := json.Marshal(person)
16+
if err != nil {
17+
http.Error(w, err.Error(), http.StatusInternalServerError)
18+
return
19+
}
20+
21+
w.Header().Set("Content-Type", "application/json")
22+
w.Write(jsonPerson)
23+
}
24+
25+
func main() {
26+
http.HandleFunc("/", hello)
27+
fmt.Println("Serving http...")
28+
http.ListenAndServe(":8000", nil)
29+
}

0 commit comments

Comments
 (0)