-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
30 lines (26 loc) · 606 Bytes
/
api.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
package main
import (
"encoding/json"
"net/http"
)
func getUsers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var data interface{}
var status int
if r.Method != "GET" {
data = struct {
Error string `json:"error"`
}{Error: "Method not allowed"}
status = http.StatusMethodNotAllowed
} else {
data = []map[string]interface{}{{"id": 1, "name": "Jack"}}
status = http.StatusOK
}
resp, _ := json.Marshal(data)
w.WriteHeader(status)
w.Write(resp)
}
func main() {
http.HandleFunc("/users", getUsers)
http.ListenAndServe(":8080", nil)
}