This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (81 loc) · 1.85 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"html/template"
"net/http"
"strconv"
)
type Env struct {
Data []int
From int
To int
Amount int
ForwardStart int
BackwardStart int
}
var tpl *template.Template
var xi []int
func init() {
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}
func main() {
xi = make([]int, 0, 100)
for i := 0; i <= 100; i++ {
xi = append(xi, i)
}
http.HandleFunc("/", index)
http.ListenAndServe(":8080", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
// we will show records FROM a certain record, TO a certain record
next := r.FormValue("next")
from, err := strconv.Atoi(next)
if err != nil {
from = 0
}
recordsPerPageToShow := 10
to := from + recordsPerPageToShow
// get the data to pass in
// slice a slice; aka, slicing a slice
// up to "to" but not including "to" is the way slicing works
// your slices could be slices of anything: int, string, struct
var xx []int
if to == len(xi) {
// get records to end
xx = xi[from:]
} else {
xx = xi[from:to]
}
// get the ForwardStart
// if we are going "from" "to"
// then the next group of records would start at "to"
// but only if there are such records
var fs int
if to+recordsPerPageToShow <= len(xi) {
fs = to
} else {
fs = len(xi) - recordsPerPageToShow
}
// get the BackwardStart
// if we are going "from" "to"
// then the previous group of records would start at "from" minus ten (records shown)
// but only if there are such records
var bs int
if from-recordsPerPageToShow >= 0 {
bs = from - recordsPerPageToShow
} else {
bs = 0
}
data := Env{
Data: xx,
From: from,
To: to - 1,
Amount: recordsPerPageToShow,
ForwardStart: fs,
BackwardStart: bs,
}
err = tpl.ExecuteTemplate(w, "index.gohtml", data)
if err != nil {
fmt.Println(err)
}
}