-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.go
44 lines (39 loc) · 818 Bytes
/
hello.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"encoding/json"
"log"
"net/http"
)
type CitiesResponse struct {
Cities []string `json:"cities"`
}
func CityHandler(res http.ResponseWriter, req *http.Request) {
citiesResponse := &CitiesResponse{
Cities: []string{
"San Francisco",
"Amsterdam",
"Berlin",
"New York",
"Tokyo",
"Kyoto",
"Osaka",
"Nagasaki",
"Naha",
"London",
"Paris",
"Seoul",
"Austin",
},
}
data, _ := json.MarshalIndent(citiesResponse, "", " ")
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func main() {
log.Println("Listening on this host: http://localhost:5005")
http.HandleFunc("/cities.json", CityHandler)
//err := http.ListenAndServe(":5005", nil)
//if err != nil {
// log.Fatal("Unable to listen on :5005: ", err)
//}
}