Mode:
Duration:
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
package main
import (
"fmt"
"net/http"
"strconv"
)
var users = []string{"Alice", "Bob", "Charlie", "David", "Eve"}
func usersHandler(w http.ResponseWriter, r *http.Request) {
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
if limit == 0 {
limit = 2
}
end := offset + limit
if end > len(users) {
end = len(users)
}
fmt.Fprintln(w, users[offset:end])
}
func main() {
http.HandleFunc("/users", usersHandler)
fmt.Println("API running on :8080")
http.ListenAndServe(":8080", nil)
}Coding works best on desktop or with an external keyboard.