-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.xml
More file actions
837 lines (640 loc) · 60 KB
/
index.xml
File metadata and controls
837 lines (640 loc) · 60 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Go Web Examples</title>
<link>https://gowebexamples.github.io/index.xml</link>
<description>Recent content on Go Web Examples</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<atom:link href="https://gowebexamples.github.io/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>Hello World</title>
<link>https://gowebexamples.github.io/hello-world/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/hello-world/</guid>
<description>
<h1 id="go-web-examples-hello-world"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Hello World</h1>
<p>This example will show how to start a webserver on port 8080 and print the classic &ldquo;hello world&rdquo; message.</p>
<p><div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// hello-world.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
)
<span style="font-weight: bold">func</span> main() {
http.HandleFunc(<span style="color: #bb8844">&quot;/&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
fmt.Fprintln(w, <span style="color: #bb8844">&quot;hello world&quot;</span>)
})
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run hello-world.go
<span style="color: #555555">$</span> curl -s http://localhost:8080/
<span style="color: #888888">hello world</span>
</pre></div>
</p>
</description>
</item>
<item>
<title>Routes</title>
<link>https://gowebexamples.github.io/routes/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/routes/</guid>
<description>
<h1 id="go-web-examples-routes"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Routes</h1>
<p>This example will show how to register a route and get the data using just the <code>net/http</code> package.</p>
<p><div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// routes.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
)
<span style="font-weight: bold">func</span> main() {
userAges <span style="font-weight: bold">:=</span> <span style="font-weight: bold">map</span>[<span style="color: #445588; font-weight: bold">string</span>]<span style="color: #445588; font-weight: bold">int</span>{
<span style="color: #bb8844">&quot;Alice&quot;</span>: <span style="color: #009999">25</span>,
<span style="color: #bb8844">&quot;Bob&quot;</span>: <span style="color: #009999">30</span>,
<span style="color: #bb8844">&quot;Claire&quot;</span>: <span style="color: #009999">29</span>,
}
http.HandleFunc(<span style="color: #bb8844">&quot;/users/&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
name <span style="font-weight: bold">:=</span> r.URL.Path[<span style="color: #999999">len</span>(<span style="color: #bb8844">&quot;/users/&quot;</span>):]
age <span style="font-weight: bold">:=</span> userAges[name]
fmt.Fprintf(w, <span style="color: #bb8844">&quot;%s is %d years old!&quot;</span>, name, age)
})
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run routes.go
<span style="color: #555555">$</span> curl -s http://localhost:8080/users/Bob
<span style="color: #888888">Bob is 30 years old!</span>
</pre></div>
</p>
</description>
</item>
<item>
<title>Routes (using gorilla/mux)</title>
<link>https://gowebexamples.github.io/routes-using-gorilla-mux/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/routes-using-gorilla-mux/</guid>
<description>
<h1 id="go-web-examples-routes-using-gorilla-mux"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Routes (using gorilla/mux)</h1>
<p>This example will show how to register routes using the popular <a target="_blank" href="https://github.com/gorilla/mux">gorilla/mux</a> router.
It is compatible with the router from the standard <code>net/http</code> package but adds a few extra features like named url parameters and http method restrictions to remove some boilerplate code.</p>
<p>To use the library we will have to install it first like so:</p>
<p><code>$ go get github.com/gorilla/mux</code></p>
<p>From now on, every application we write will be able to make use of this library.</p>
<p><div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// routes.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
<span style="color: #bb8844">&quot;github.com/gorilla/mux&quot;</span>
)
<span style="font-weight: bold">func</span> main() {
userAges <span style="font-weight: bold">:=</span> <span style="font-weight: bold">map</span>[<span style="color: #445588; font-weight: bold">string</span>]<span style="color: #445588; font-weight: bold">int</span>{
<span style="color: #bb8844">&quot;Alice&quot;</span>: <span style="color: #009999">25</span>,
<span style="color: #bb8844">&quot;Bob&quot;</span>: <span style="color: #009999">30</span>,
<span style="color: #bb8844">&quot;Claire&quot;</span>: <span style="color: #009999">29</span>,
}
r <span style="font-weight: bold">:=</span> mux.NewRouter()
r.HandleFunc(<span style="color: #bb8844">&quot;/users/{name}&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
vars <span style="font-weight: bold">:=</span> mux.Vars(r)
name <span style="font-weight: bold">:=</span> vars[<span style="color: #bb8844">&quot;name&quot;</span>]
age <span style="font-weight: bold">:=</span> userAges[name]
fmt.Fprintf(w, <span style="color: #bb8844">&quot;%s is %d years old!&quot;</span>, name, age)
}).Methods(<span style="color: #bb8844">&quot;GET&quot;</span>)
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, r)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run routes.go
<span style="color: #555555">$</span> curl -s http://localhost:8080/users/Bob
<span style="color: #888888">Bob is 30 years old!</span>
</pre></div>
</p>
</description>
</item>
<item>
<title>Templates</title>
<link>https://gowebexamples.github.io/templates/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/templates/</guid>
<description>
<h1 id="go-web-examples-templates"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Templates</h1>
<p>This example will show how to render a simple list of TODO items into an html page using the <code>html/template</code> package.</p>
<p><div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// todos.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;html/template&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
)
<span style="font-weight: bold">type</span> Todo <span style="font-weight: bold">struct</span> {
Task <span style="color: #445588; font-weight: bold">string</span>
Done <span style="color: #445588; font-weight: bold">bool</span>
}
<span style="font-weight: bold">func</span> main() {
tmpl <span style="font-weight: bold">:=</span> template.Must(template.ParseFiles(<span style="color: #bb8844">&quot;todos.html&quot;</span>))
todos <span style="font-weight: bold">:=</span> []Todo{
{<span style="color: #bb8844">&quot;Learn Go&quot;</span>, <span style="font-weight: bold">true</span>},
{<span style="color: #bb8844">&quot;Read Go Web Examples&quot;</span>, <span style="font-weight: bold">true</span>},
{<span style="color: #bb8844">&quot;Create a web app in Go&quot;</span>, <span style="font-weight: bold">false</span>},
}
http.HandleFunc(<span style="color: #bb8844">&quot;/&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
tmpl.Execute(w, <span style="font-weight: bold">struct</span>{ Todos []Todo }{todos})
})
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">&lt;!-- todos.html --&gt;</span>
&lt;<span style="color: #000080">h1</span>&gt;Todos&lt;/<span style="color: #000080">h1</span>&gt;
&lt;<span style="color: #000080">ul</span>&gt;
{{range .Todos}}
{{if .Done}}
&lt;<span style="color: #000080">li</span>&gt;&lt;<span style="color: #000080">s</span>&gt;{{.Task}}&lt;/<span style="color: #000080">s</span>&gt;&lt;/<span style="color: #000080">li</span>&gt;
{{else}}
&lt;<span style="color: #000080">li</span>&gt;{{.Task}}&lt;/<span style="color: #000080">li</span>&gt;
{{end}}
{{end}}
&lt;/<span style="color: #000080">ul</span>&gt;
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run todos.go
</pre></div>
<div class="demo">
<h1>Todos</h1>
<ul>
<li><s>Learn Go</s></li>
<li><s>Read Go Web Examples</s></li>
<li>Create a web app in Go</li>
</ul>
</div></p>
</description>
</item>
<item>
<title>Forms</title>
<link>https://gowebexamples.github.io/forms/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/forms/</guid>
<description>
<h1 id="go-web-examples-forms"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Forms</h1>
<p>This example will show how to simulate a contact form and parse the message into a struct.</p>
<p><div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// forms.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;html/template&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
)
<span style="font-weight: bold">type</span> ContactDetails <span style="font-weight: bold">struct</span> {
Email <span style="color: #445588; font-weight: bold">string</span>
Subject <span style="color: #445588; font-weight: bold">string</span>
Message <span style="color: #445588; font-weight: bold">string</span>
}
<span style="font-weight: bold">func</span> main() {
tmpl <span style="font-weight: bold">:=</span> template.Must(template.ParseFiles(<span style="color: #bb8844">&quot;forms.html&quot;</span>))
http.HandleFunc(<span style="color: #bb8844">&quot;/&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
<span style="font-weight: bold">if</span> r.Method <span style="font-weight: bold">!=</span> http.MethodPost {
tmpl.Execute(w, <span style="font-weight: bold">nil</span>)
<span style="font-weight: bold">return</span>
}
details <span style="font-weight: bold">:=</span> ContactDetails{
Email: r.FormValue(<span style="color: #bb8844">&quot;email&quot;</span>),
Subject: r.FormValue(<span style="color: #bb8844">&quot;subject&quot;</span>),
Message: r.FormValue(<span style="color: #bb8844">&quot;message&quot;</span>),
}
<span style="color: #999988; font-style: italic">// do something with details</span>
_ = details
tmpl.Execute(w, <span style="font-weight: bold">struct</span>{ Success <span style="color: #445588; font-weight: bold">bool</span> }{<span style="font-weight: bold">true</span>})
})
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">&lt;!-- forms.html --&gt;</span>
{{if .Success}}
&lt;<span style="color: #000080">h1</span>&gt;Thanks for your message!&lt;/<span style="color: #000080">h1</span>&gt;
{{else}}
&lt;<span style="color: #000080">h1</span>&gt;Contact&lt;/<span style="color: #000080">h1</span>&gt;
&lt;<span style="color: #000080">form</span> <span style="color: #008080">method</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;POST&quot;</span>&gt;
&lt;<span style="color: #000080">label</span>&gt;Email:&lt;/<span style="color: #000080">label</span>&gt;&lt;<span style="color: #000080">br</span> /&gt;
&lt;<span style="color: #000080">input</span> <span style="color: #008080">type</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;text&quot;</span> <span style="color: #008080">name</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;email&quot;</span>&gt;&lt;<span style="color: #000080">br</span> /&gt;
&lt;<span style="color: #000080">label</span>&gt;Subject:&lt;/<span style="color: #000080">label</span>&gt;&lt;<span style="color: #000080">br</span> /&gt;
&lt;<span style="color: #000080">input</span> <span style="color: #008080">type</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;text&quot;</span> <span style="color: #008080">name</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;subject&quot;</span>&gt;&lt;<span style="color: #000080">br</span> /&gt;
&lt;<span style="color: #000080">label</span>&gt;Message:&lt;/<span style="color: #000080">label</span>&gt;&lt;<span style="color: #000080">br</span> /&gt;
&lt;<span style="color: #000080">textarea</span> <span style="color: #008080">name</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;message&quot;</span>&gt;&lt;/<span style="color: #000080">textarea</span>&gt;&lt;<span style="color: #000080">br</span> /&gt;
&lt;<span style="color: #000080">input</span> <span style="color: #008080">type</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;submit&quot;</span>&gt;
&lt;/<span style="color: #000080">form</span>&gt;
{{end}}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run forms.go
</pre></div>
<div class="demo">
<h1>Contact</h1>
<form method="POST">
<label>Email:</label><br />
<input type="text" name="email"><br />
<label>Subject:</label><br />
<input type="text" name="subject"><br />
<label>Message:</label><br />
<textarea name="message"></textarea><br />
<input type="submit">
</form>
</div></p>
</description>
</item>
<item>
<title>JSON</title>
<link>https://gowebexamples.github.io/json/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/json/</guid>
<description>
<h1 id="go-web-examples-json"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> JSON</h1>
<p>This example will show how to encode and decode JSON data using the <code>encoding/json</code> package.
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// json.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;encoding/json&quot;</span>
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
)
<span style="font-weight: bold">type</span> User <span style="font-weight: bold">struct</span> {
Firstname <span style="color: #445588; font-weight: bold">string</span> <span style="color: #bb8844">`json:&quot;firstname&quot;`</span>
Lastname <span style="color: #445588; font-weight: bold">string</span> <span style="color: #bb8844">`json:&quot;lastname&quot;`</span>
Age <span style="color: #445588; font-weight: bold">int</span> <span style="color: #bb8844">`json:&quot;age&quot;`</span>
}
<span style="font-weight: bold">func</span> main() {
http.HandleFunc(<span style="color: #bb8844">&quot;/decode&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
<span style="font-weight: bold">var</span> user User
json.NewDecoder(r.Body).Decode(<span style="font-weight: bold">&amp;</span>user)
fmt.Fprintf(w, <span style="color: #bb8844">&quot;%s %s is %d years old!&quot;</span>, user.Firstname, user.Lastname, user.Age)
})
http.HandleFunc(<span style="color: #bb8844">&quot;/encode&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
peter <span style="font-weight: bold">:=</span> User{
Firstname: <span style="color: #bb8844">&quot;John&quot;</span>,
Lastname: <span style="color: #bb8844">&quot;Doe&quot;</span>,
Age: <span style="color: #009999">25</span>,
}
json.NewEncoder(w).Encode(peter)
})
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run json.go
<span style="color: #555555">$</span> curl -s -XPOST -d<span style="color: #bb8844">&#39;{&quot;firstname&quot;:&quot;Donald&quot;,&quot;lastname&quot;:&quot;Trump&quot;,&quot;age&quot;:70}&#39;</span> http://localhost:8080/decode
<span style="color: #888888">Donald Trump is 70 years old!</span>
<span style="color: #555555">$</span> curl -s http://localhost:8080/encode
<span style="color: #888888">{&quot;firstname&quot;:&quot;John&quot;,&quot;lastname&quot;:&quot;Doe&quot;,&quot;age&quot;:25}</span>
</pre></div>
</p>
</description>
</item>
<item>
<title>Static Files</title>
<link>https://gowebexamples.github.io/static-files/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/static-files/</guid>
<description>
<h1 id="go-web-examples-static-files"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Static Files</h1>
<p>This example will show how to serve static files like CSSs, JavaScripts or images from a specific directory.</p>
<p><div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// static-files.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> <span style="color: #bb8844">&quot;net/http&quot;</span>
<span style="font-weight: bold">func</span> main() {
fs <span style="font-weight: bold">:=</span> http.FileServer(http.Dir(<span style="color: #bb8844">&quot;assets/&quot;</span>))
http.Handle(<span style="color: #bb8844">&quot;/static/&quot;</span>, http.StripPrefix(<span style="color: #bb8844">&quot;/static/&quot;</span>, fs))
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> tree assets/
<span style="color: #888888">assets/</span>
<span style="color: #888888">└── css</span>
<span style="color: #888888"> └── styles.css</span>
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run static-files.go
<span style="color: #555555">$</span> curl -s http://localhost:8080/static/css/styles.css
<span style="color: #888888">body {</span>
<span style="color: #888888"> background-color: black;</span>
<span style="color: #888888">}</span>
</pre></div>
</p>
</description>
</item>
<item>
<title>Sessions</title>
<link>https://gowebexamples.github.io/sessions/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/sessions/</guid>
<description>
<h1 id="go-web-examples-sessions"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Sessions</h1>
<p>This example will show how to store data in session cookies using the popular <a target="_blank" href="https://github.com/gorilla/sessions">gorilla/sessions</a> package in Go.</p>
<p>Cookies are small pieces of data stored in the browser of a user and are sent to our server on each request. In them, we can store e.g. whether or not a user is logged in into our website and figure out who he actually is (in our system).</p>
<p>In this example we will only allow authenticated users to view our secret message on the <code>/secret</code> page. To get access to it, the will first have to visit <code>/login</code> to get a valid session cookie, which logs him in. Additionally he can visit <code>/logout</code> to revoke his access to our secret message.
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// sessions.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
<span style="color: #bb8844">&quot;github.com/gorilla/sessions&quot;</span>
)
<span style="font-weight: bold">var</span> (
<span style="color: #999988; font-style: italic">// key must be 16, 24 or 32 bytes long (AES-128, AES-192 or AES-256)</span>
key = []<span style="color: #999999">byte</span>(<span style="color: #bb8844">&quot;super-secret-key&quot;</span>)
store = sessions.NewCookieStore(key)
)
<span style="font-weight: bold">func</span> secret(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
session, _ <span style="font-weight: bold">:=</span> store.Get(r, <span style="color: #bb8844">&quot;cookie-name&quot;</span>)
<span style="color: #999988; font-style: italic">// Check if user is authenticated</span>
<span style="font-weight: bold">if</span> auth, ok <span style="font-weight: bold">:=</span> session.Values[<span style="color: #bb8844">&quot;authenticated&quot;</span>].(<span style="color: #445588; font-weight: bold">bool</span>); !ok <span style="font-weight: bold">||</span> !auth {
http.Error(w, <span style="color: #bb8844">&quot;Forbidden&quot;</span>, http.StatusForbidden)
<span style="font-weight: bold">return</span>
}
<span style="color: #999988; font-style: italic">// Print secret message</span>
fmt.Fprintln(w, <span style="color: #bb8844">&quot;The cake is a lie!&quot;</span>)
}
<span style="font-weight: bold">func</span> login(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
session, _ <span style="font-weight: bold">:=</span> store.Get(r, <span style="color: #bb8844">&quot;cookie-name&quot;</span>)
<span style="color: #999988; font-style: italic">// Authentication goes here</span>
<span style="color: #999988; font-style: italic">// ...</span>
<span style="color: #999988; font-style: italic">// Set user as authenticated</span>
session.Values[<span style="color: #bb8844">&quot;authenticated&quot;</span>] = <span style="font-weight: bold">true</span>
session.Save(r, w)
}
<span style="font-weight: bold">func</span> logout(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
session, _ <span style="font-weight: bold">:=</span> store.Get(r, <span style="color: #bb8844">&quot;cookie-name&quot;</span>)
<span style="color: #999988; font-style: italic">// Revoke users authentication</span>
session.Values[<span style="color: #bb8844">&quot;authenticated&quot;</span>] = <span style="font-weight: bold">false</span>
session.Save(r, w)
}
<span style="font-weight: bold">func</span> main() {
http.HandleFunc(<span style="color: #bb8844">&quot;/secret&quot;</span>, secret)
http.HandleFunc(<span style="color: #bb8844">&quot;/login&quot;</span>, login)
http.HandleFunc(<span style="color: #bb8844">&quot;/logout&quot;</span>, logout)
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run sessions.go
<span style="color: #555555">$</span> curl -s http://localhost:8080/secret
<span style="color: #888888">Forbidden</span>
<span style="color: #555555">$</span> curl -s -I http://localhost:8080/login
<span style="color: #888888">Set-Cookie: cookie-name=MTQ4NzE5Mz...</span>
<span style="color: #555555">$</span> curl -s --cookie <span style="color: #bb8844">&quot;cookie-name=MTQ4NzE5Mz...&quot;</span> http://localhost:8080/secret
<span style="color: #888888">The cake is a lie!</span>
</pre></div>
</p>
</description>
</item>
<item>
<title>Password Hashing</title>
<link>https://gowebexamples.github.io/password-hashing/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/password-hashing/</guid>
<description>
<h1 id="go-web-examples-password-hashing"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Password Hashing</h1>
<p>This example will show how to hash passwords using bcrypt.
For this we have to <code>go get</code> the golang bcrypt library like so:</p>
<p><code>$ go get golang.org/x/crypto/bcrypt</code></p>
<p>From now on, every application we write will be able to make use of this library.
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// passwords.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;golang.org/x/crypto/bcrypt&quot;</span>
)
<span style="font-weight: bold">func</span> HashPassword(password <span style="color: #445588; font-weight: bold">string</span>) (<span style="color: #445588; font-weight: bold">string</span>, <span style="color: #445588; font-weight: bold">error</span>) {
bytes, err <span style="font-weight: bold">:=</span> bcrypt.GenerateFromPassword([]<span style="color: #999999">byte</span>(password), <span style="color: #009999">14</span>)
<span style="font-weight: bold">return</span> <span style="color: #999999">string</span>(bytes), err
}
<span style="font-weight: bold">func</span> CheckPasswordHash(password, hash <span style="color: #445588; font-weight: bold">string</span>) <span style="color: #445588; font-weight: bold">bool</span> {
err <span style="font-weight: bold">:=</span> bcrypt.CompareHashAndPassword([]<span style="color: #999999">byte</span>(hash), []<span style="color: #999999">byte</span>(password))
<span style="font-weight: bold">return</span> err <span style="font-weight: bold">==</span> <span style="font-weight: bold">nil</span>
}
<span style="font-weight: bold">func</span> main() {
password <span style="font-weight: bold">:=</span> <span style="color: #bb8844">&quot;secret&quot;</span>
hash, _ <span style="font-weight: bold">:=</span> HashPassword(password) <span style="color: #999988; font-style: italic">// ignore error for the sake of simplicity</span>
fmt.Println(<span style="color: #bb8844">&quot;Password:&quot;</span>, password)
fmt.Println(<span style="color: #bb8844">&quot;Hash: &quot;</span>, hash)
match <span style="font-weight: bold">:=</span> CheckPasswordHash(password, hash)
fmt.Println(<span style="color: #bb8844">&quot;Match: &quot;</span>, match)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run passwords.go
<span style="color: #888888">Password: secret</span>
<span style="color: #888888">Hash: $2a$14$ajq8Q7fbtFRQvXpdCq7Jcuy.Rx1h/L4J60Otx.gyNLbAYctGMJ9tK</span>
<span style="color: #888888">Match: true</span>
</pre></div>
</p>
</description>
</item>
<item>
<title>Websockets</title>
<link>https://gowebexamples.github.io/websockets/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/websockets/</guid>
<description>
<h1 id="go-web-examples-websockets"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Websockets</h1>
<p>This example will show how to work with websockets in Go. We will build a simple server which echoes back everything we send to it.
For this we have to <code>go get</code> the popular <a target="_blank" href="https://github.com/gorilla/websocket">gorilla/websocket</a> library like so:</p>
<p><code>$ go get github.com/gorilla/websocket</code></p>
<p>From now on, every application we write will be able to make use of this library.
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// websockets.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
<span style="color: #bb8844">&quot;github.com/gorilla/websocket&quot;</span>
)
<span style="font-weight: bold">var</span> upgrader = websocket.Upgrader{
ReadBufferSize: <span style="color: #009999">1024</span>,
WriteBufferSize: <span style="color: #009999">1024</span>,
}
<span style="font-weight: bold">func</span> main() {
http.HandleFunc(<span style="color: #bb8844">&quot;/echo&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
conn, _ <span style="font-weight: bold">:=</span> upgrader.Upgrade(w, r, <span style="font-weight: bold">nil</span>) <span style="color: #999988; font-style: italic">// error ignored for sake of simplicity</span>
<span style="font-weight: bold">for</span> {
<span style="color: #999988; font-style: italic">// Read message from browser</span>
msgType, msg, err <span style="font-weight: bold">:=</span> conn.ReadMessage()
<span style="font-weight: bold">if</span> err <span style="font-weight: bold">!=</span> <span style="font-weight: bold">nil</span> {
<span style="font-weight: bold">return</span>
}
<span style="color: #999988; font-style: italic">// Print the message to the console</span>
fmt.Printf(<span style="color: #bb8844">&quot;%s sent: %s\n&quot;</span>, conn.RemoteAddr(), <span style="color: #999999">string</span>(msg))
<span style="color: #999988; font-style: italic">// Write message back to browser</span>
<span style="font-weight: bold">if</span> err = conn.WriteMessage(msgType, msg); err <span style="font-weight: bold">!=</span> <span style="font-weight: bold">nil</span> {
<span style="font-weight: bold">return</span>
}
}
})
http.HandleFunc(<span style="color: #bb8844">&quot;/&quot;</span>, <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
http.ServeFile(w, r, <span style="color: #bb8844">&quot;websockets.html&quot;</span>)
})
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">&lt;!-- websockets.html --&gt;</span>
&lt;<span style="color: #000080">input</span> <span style="color: #008080">id</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;input&quot;</span> <span style="color: #008080">type</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;text&quot;</span> /&gt;
&lt;<span style="color: #000080">button</span> <span style="color: #008080">onclick</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;send()&quot;</span>&gt;Send&lt;/<span style="color: #000080">button</span>&gt;
&lt;<span style="color: #000080">pre</span> <span style="color: #008080">id</span><span style="font-weight: bold">=</span><span style="color: #bb8844">&quot;output&quot;</span>&gt;&lt;/<span style="color: #000080">pre</span>&gt;
&lt;<span style="color: #000080">script</span>&gt;
<span style="font-weight: bold">var</span> input <span style="font-weight: bold">=</span> <span style="color: #999999">document</span>.getElementById(<span style="color: #bb8844">&quot;input&quot;</span>);
<span style="font-weight: bold">var</span> output <span style="font-weight: bold">=</span> <span style="color: #999999">document</span>.getElementById(<span style="color: #bb8844">&quot;output&quot;</span>);
<span style="font-weight: bold">var</span> socket <span style="font-weight: bold">=</span> <span style="font-weight: bold">new</span> WebSocket(<span style="color: #bb8844">&quot;ws://localhost:8080/echo&quot;</span>);
socket.onopen <span style="font-weight: bold">=</span> <span style="font-weight: bold">function</span> () {
output.innerHTML <span style="font-weight: bold">+=</span> <span style="color: #bb8844">&quot;Status: Connected\n&quot;</span>;
};
socket.onmessage <span style="font-weight: bold">=</span> <span style="font-weight: bold">function</span> (e) {
output.innerHTML <span style="font-weight: bold">+=</span> <span style="color: #bb8844">&quot;Server: &quot;</span> <span style="font-weight: bold">+</span> e.data <span style="font-weight: bold">+</span> <span style="color: #bb8844">&quot;\n&quot;</span>;
};
<span style="font-weight: bold">function</span> send() {
socket.send(input.value);
input.value <span style="font-weight: bold">=</span> <span style="color: #bb8844">&quot;&quot;</span>;
}
&lt;/<span style="color: #000080">script</span>&gt;
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run websockets.go
<span style="color: #888888">[127.0.0.1]:53403 sent: Hello Go Web Examples, you&#39;re doing great!</span>
</pre></div>
<div class="demo">
<input type="text">
<button>Send</button>
<pre>Status: Connected
Server: Hello Go Web Examples, you&rsquo;re doing great!</pre>
</div></p>
</description>
</item>
<item>
<title>Middleware (Basic)</title>
<link>https://gowebexamples.github.io/basic-middleware/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/basic-middleware/</guid>
<description>
<h1 id="go-web-examples-middleware-basic"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Middleware (Basic)</h1>
<p>This example will show how to create basic logging middleware in Go.</p>
<p>A middleware simply takes a <code>http.HandlerFunc</code> as one of its parameters, wraps it and returns a new <code>http.HandlerFunc</code> for the server to call.</p>
<p><div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// basic-middleware.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;log&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
)
<span style="font-weight: bold">func</span> logging(f http.HandlerFunc) http.HandlerFunc {
<span style="font-weight: bold">return</span> <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
log.Println(r.URL.Path)
f(w, r)
}
}
<span style="font-weight: bold">func</span> foo(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
fmt.Fprintln(w, <span style="color: #bb8844">&quot;foo&quot;</span>)
}
<span style="font-weight: bold">func</span> bar(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
fmt.Fprintln(w, <span style="color: #bb8844">&quot;bar&quot;</span>)
}
<span style="font-weight: bold">func</span> main() {
http.HandleFunc(<span style="color: #bb8844">&quot;/foo&quot;</span>, logging(foo))
http.HandleFunc(<span style="color: #bb8844">&quot;/bar&quot;</span>, logging(bar))
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run basic-middleware.go
<span style="color: #888888">2017/02/10 23:59:34 /foo</span>
<span style="color: #888888">2017/02/10 23:59:35 /bar</span>
<span style="color: #888888">2017/02/10 23:59:36 /foo?bar</span>
<span style="color: #555555">$</span> curl -s http://localhost:8080/foo
<span style="color: #555555">$</span> curl -s http://localhost:8080/bar
<span style="color: #555555">$</span> curl -s http://localhost:8080/foo?bar
</pre></div>
</p>
</description>
</item>
<item>
<title>Middleware (Advanced)</title>
<link>https://gowebexamples.github.io/advanced-middleware/</link>
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
<guid>https://gowebexamples.github.io/advanced-middleware/</guid>
<description>
<h1 id="go-web-examples-middleware-advanced"><a href="https://gowebexamples.github.io/">Go Web Examples:</a> Middleware (Advanced)</h1>
<p>This example will show how to create a more advanced version of middleware in Go.</p>
<p>A middleware in itself simply takes a <code>http.HandlerFunc</code> as one of its parameters, wraps it and returns a new <code>http.HandlerFunc</code> for the server to call.</p>
<p>Here we define a new type <code>Middleware</code> which makes it eventually easier to chain multiple middlewares together. This idea is inspired by Mat Ryers&rsquo; talk about Building APIs.
You can find a more detailed explaination including the talk <a target="_blank" href="https://medium.com/@matryer/writing-middleware-in-golang-and-how-go-makes-it-so-much-fun-4375c1246e81">here</a>.</p>
<p><br />
This snippet explains in detail how a new middleware is created. In the full example below, we reduce this version by some boilerplate code.
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="font-weight: bold">func</span> createNewMiddleware() Middleware {
<span style="color: #999988; font-style: italic">// Create a new Middleware</span>
middleware <span style="font-weight: bold">:=</span> <span style="font-weight: bold">func</span>(next http.HandlerFunc) http.HandlerFunc {
<span style="color: #999988; font-style: italic">// Define the http.HandlerFunc which is called by the server eventually</span>
handler <span style="font-weight: bold">:=</span> <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
<span style="color: #999988; font-style: italic">// ... do middleware things</span>
<span style="color: #999988; font-style: italic">// Call the next middleware/handler in chain</span>
next(w, r)
}
<span style="color: #999988; font-style: italic">// Return newly created handler</span>
<span style="font-weight: bold">return</span> handler
}
<span style="color: #999988; font-style: italic">// Return newly created middleware</span>
<span style="font-weight: bold">return</span> middleware
}
</pre></div>
<br />
This is the full example:
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #999988; font-style: italic">// advanced-middleware.go</span>
<span style="font-weight: bold">package</span> main
<span style="font-weight: bold">import</span> (
<span style="color: #bb8844">&quot;fmt&quot;</span>
<span style="color: #bb8844">&quot;log&quot;</span>
<span style="color: #bb8844">&quot;net/http&quot;</span>
<span style="color: #bb8844">&quot;time&quot;</span>
)
<span style="font-weight: bold">type</span> Middleware <span style="font-weight: bold">func</span>(http.HandlerFunc) http.HandlerFunc
<span style="color: #999988; font-style: italic">// Logging logs all requests with its path and the time it took to process</span>
<span style="font-weight: bold">func</span> Logging() Middleware {
<span style="color: #999988; font-style: italic">// Create a new Middleware</span>
<span style="font-weight: bold">return</span> <span style="font-weight: bold">func</span>(f http.HandlerFunc) http.HandlerFunc {
<span style="color: #999988; font-style: italic">// Define the http.HandlerFunc</span>
<span style="font-weight: bold">return</span> <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
<span style="color: #999988; font-style: italic">// Do middleware things</span>
start <span style="font-weight: bold">:=</span> time.Now()
<span style="font-weight: bold">defer</span> <span style="font-weight: bold">func</span>() { log.Println(r.URL.Path, time.Since(start)) }()
<span style="color: #999988; font-style: italic">// Call the next middleware/handler in chain</span>
f(w, r)
}
}
}
<span style="color: #999988; font-style: italic">// Method ensures that url can only be requested with a specific method, else returns a 400 Bad Request</span>
<span style="font-weight: bold">func</span> Method(m <span style="color: #445588; font-weight: bold">string</span>) Middleware {
<span style="color: #999988; font-style: italic">// Create a new Middleware</span>
<span style="font-weight: bold">return</span> <span style="font-weight: bold">func</span>(f http.HandlerFunc) http.HandlerFunc {
<span style="color: #999988; font-style: italic">// Define the http.HandlerFunc</span>
<span style="font-weight: bold">return</span> <span style="font-weight: bold">func</span>(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
<span style="color: #999988; font-style: italic">// Do middleware things</span>
<span style="font-weight: bold">if</span> r.Method <span style="font-weight: bold">!=</span> m {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
<span style="font-weight: bold">return</span>
}
<span style="color: #999988; font-style: italic">// Call the next middleware/handler in chain</span>
f(w, r)
}
}
}
<span style="color: #999988; font-style: italic">// Chain applies middlewares to a http.HandlerFunc</span>
<span style="font-weight: bold">func</span> Chain(f http.HandlerFunc, middlewares <span style="font-weight: bold">...</span>Middleware) http.HandlerFunc {
<span style="font-weight: bold">for</span> _, m <span style="font-weight: bold">:=</span> <span style="font-weight: bold">range</span> middlewares {
f = m(f)
}
<span style="font-weight: bold">return</span> f
}
<span style="font-weight: bold">func</span> Hello(w http.ResponseWriter, r <span style="font-weight: bold">*</span>http.Request) {
fmt.Fprintln(w, <span style="color: #bb8844">&quot;hello world&quot;</span>)
}
<span style="font-weight: bold">func</span> main() {
http.HandleFunc(<span style="color: #bb8844">&quot;/&quot;</span>, Chain(Hello, Method(<span style="color: #bb8844">&quot;GET&quot;</span>), Logging()))
http.ListenAndServe(<span style="color: #bb8844">&quot;:8080&quot;</span>, <span style="font-weight: bold">nil</span>)
}
</pre></div>
<div class="highlight" style="background: #ffffff"><pre style="line-height: 125%"><span></span><span style="color: #555555">$</span> go run advanced-middleware.go
<span style="color: #888888">2017/02/11 00:34:53 / 0s</span>
<span style="color: #555555">$</span> curl -s http://localhost:8080/
<span style="color: #888888">hello world</span>
<span style="color: #555555">$</span> curl -s -XPOST http://localhost:8080/
<span style="color: #888888">Bad Request</span>
</pre></div>
</p>
</description>
</item>
</channel>
</rss>