-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_long_method.go
45 lines (38 loc) · 848 Bytes
/
03_long_method.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
45
package welcome
import (
"database/sql"
"encoding/json"
"net/http"
"strconv"
)
func longMethod(resp http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
resp.WriteHeader(http.StatusPreconditionFailed)
return
}
userID, err := strconv.ParseInt(req.Form.Get("UserID"), 10, 64)
if err != nil {
resp.WriteHeader(http.StatusPreconditionFailed)
return
}
row := DB.QueryRow("SELECT * FROM people WHERE ID = ?", userID)
person := &Person{}
err = row.Scan(&person.ID, &person.Name, &person.Phone)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
return
}
encoder := json.NewEncoder(resp)
err = encoder.Encode(person)
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
return
}
}
var DB *sql.DB
type Person struct {
ID int64
Name string
Phone string
}