package main
import (
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type Item struct {
ID int `json:"id"`
Name string `json:"name"`
}
var items []Item
func main() {
for i := 1; i <= 100; i++ {
items = append(items, Item{ID: i, Name: "Item " + strconv.Itoa(i)})
}
r := mux.NewRouter()
r.HandleFunc("/items", getItems).Methods("GET")
http.ListenAndServe(":8080", r)
}
func getItems(w http.ResponseWriter, r *http.Request) {
pageStr := r.URL.Query().Get("page")
limitStr := r.URL.Query().Get("limit")
page, _ := strconv.Atoi(pageStr)
limit, _ := strconv.Atoi(limitStr)
if page < 1 {
page = 1
}
if limit < 1 {
limit = 10 // default limit
}
start := (page - 1) * limit
end := start + limit
if start >= len(items) {
start = len(items)
}
if end > len(items) {
end = len(items)
}
json.NewEncoder(w).Encode(items[start:end])
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?