This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (163 loc) · 5.36 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"sort"
"time"
)
//!+main
type Track struct {
Title string
Artist string
Album string
Year int
Length time.Duration
}
var tracks = []*Track{
{"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")},
{"Go", "Moby", "Moby", 1992, length("3m37s")},
{"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")},
{"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")},
}
func length(s string) time.Duration {
d, err := time.ParseDuration(s)
if err != nil {
panic(s)
}
return d
}
type byTitle []*Track
func (x byTitle) Len() int { return len(x) }
func (x byTitle) Less(i, j int) bool { return x[i].Title < x[j].Title }
func (x byTitle) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
type byArtist []*Track
func (x byArtist) Len() int { return len(x) }
func (x byArtist) Less(i, j int) bool { return x[i].Artist < x[j].Artist }
func (x byArtist) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
type byAlbum []*Track
func (x byAlbum) Len() int { return len(x) }
func (x byAlbum) Less(i, j int) bool { return x[i].Album < x[j].Album }
func (x byAlbum) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
type byYear []*Track
func (x byYear) Len() int { return len(x) }
func (x byYear) Less(i, j int) bool { return x[i].Year < x[j].Year }
func (x byYear) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
type byLength []*Track
func (x byLength) Len() int { return len(x) }
func (x byLength) Less(i, j int) bool { return x[i].Length < x[j].Length }
func (x byLength) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func main() {
sort.Sort(byArtist(tracks))
http.HandleFunc("/", handleRoot)
fmt.Println("usage:")
fmt.Println("http://localhost:8000/?sort=title")
fmt.Println("http://localhost:8000/?sort=artist")
fmt.Println("http://localhost:8000/?sort=album")
fmt.Println("http://localhost:8000/?sort=year")
fmt.Println("http://localhost:8000/?sort=length")
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
func handleRoot(w http.ResponseWriter, req *http.Request) {
s := req.URL.Query().Get("sort")
if s != "" {
switch s {
case "title":
sort.Sort(byTitle(tracks))
case "artist":
sort.Sort(byArtist(tracks))
case "album":
sort.Sort(byAlbum(tracks))
case "year":
sort.Sort(byYear(tracks))
case "length":
sort.Sort(byLength(tracks))
default:
w.WriteHeader(http.StatusBadRequest) // 400
fmt.Fprintf(w, "unsuported sort key: %q\n", s)
}
}
t := template.Must(template.New("track").Parse(`
<table>
<thead>
<tr>
<th><a href="/?sort=title">Title</a></th>
<th><a href="/?sort=artist">Artist</a></th>
<th><a href="/?sort=album">Album</a></th>
<th><a href="/?sort=year">Year</a></th>
<th><a href="/?sort=length">Length</a></th>
</tr>
</thead>
<tbody>
{{range .}}
<tr>
<td>{{.Title}}</td>
<td>{{.Artist}}</td>
<td>{{.Album}}</td>
<td>{{.Year}}</td>
<td>{{.Length}}</td>
</tr>
{{end}}
</tbody>
</table>`))
if err := t.Execute(w, tracks); err != nil {
w.WriteHeader(http.StatusInternalServerError) // 500
log.Println(err)
fmt.Fprintln(w, "Internal Server Error")
}
}
/*
//!+artistoutput
Title Artist Album Year Length
----- ------ ----- ---- ------
Go Ahead Alicia Keys As I Am 2007 4m36s
Go Delilah From the Roots Up 2012 3m38s
Ready 2 Go Martin Solveig Smash 2011 4m24s
Go Moby Moby 1992 3m37s
//!-artistoutput
//!+artistrevoutput
Title Artist Album Year Length
----- ------ ----- ---- ------
Go Moby Moby 1992 3m37s
Ready 2 Go Martin Solveig Smash 2011 4m24s
Go Delilah From the Roots Up 2012 3m38s
Go Ahead Alicia Keys As I Am 2007 4m36s
//!-artistrevoutput
//!+yearoutput
Title Artist Album Year Length
----- ------ ----- ---- ------
Go Moby Moby 1992 3m37s
Go Ahead Alicia Keys As I Am 2007 4m36s
Ready 2 Go Martin Solveig Smash 2011 4m24s
Go Delilah From the Roots Up 2012 3m38s
//!-yearoutput
//!+customout
Title Artist Album Year Length
----- ------ ----- ---- ------
Go Moby Moby 1992 3m37s
Go Delilah From the Roots Up 2012 3m38s
Go Ahead Alicia Keys As I Am 2007 4m36s
Ready 2 Go Martin Solveig Smash 2011 4m24s
//!-customout
*/
//!+customcode
type customSort struct {
t []*Track
less func(x, y *Track) bool
}
func (x customSort) Len() int { return len(x.t) }
func (x customSort) Less(i, j int) bool { return x.less(x.t[i], x.t[j]) }
func (x customSort) Swap(i, j int) { x.t[i], x.t[j] = x.t[j], x.t[i] }
func init() {
//!+ints
values := []int{3, 1, 4, 1}
fmt.Println(sort.IntsAreSorted(values)) // "false"
sort.Ints(values)
fmt.Println(values) // "[1 1 3 4]"
fmt.Println(sort.IntsAreSorted(values)) // "true"
sort.Sort(sort.Reverse(sort.IntSlice(values)))
fmt.Println(values) // "[4 3 1 1]"
fmt.Println(sort.IntsAreSorted(values)) // "false"
//!-ints
}