File tree Expand file tree Collapse file tree 3 files changed +65
-0
lines changed
examples/gocollective/retrive-values-from-url Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 55 init : go install honnef.co/go/tools/cmd/staticcheck@latest
66 - name : install go-task
77 init : go install github.com/go-task/task/v3/cmd/task@latest
8+ - name : install goimport
9+ init : go install golang.org/x/tools/cmd/goimports@latest
810 - name : build app
911 command : task build
1012
Original file line number Diff line number Diff line change 1+ version : ' 3'
2+
3+ tasks :
4+ imports :
5+ cmds :
6+ - goimports -w .
7+ vet :
8+ cmds :
9+ - go vet .
10+ run :
11+ deps : [
12+ imports, vet
13+ ]
14+ cmds :
15+ - go run -race main.go
Original file line number Diff line number Diff line change 1+ // Stackoverflow Go Collective example
2+ //
3+ // How to retrieve values from URL
4+ //
5+ // URL
6+ // - https://stackoverflow.com/questions/73079531/how-to-retrieve-values-from-the-url-in-go
7+ //
8+ // REFERENCES
9+ // - https://pkg.go.dev/net/http@latest
10+ // - https://stackoverflow.com/questions/39320025/how-to-stop-http-listenandserve
11+ package main
12+
13+ import (
14+ "context"
15+ "fmt"
16+ "net/http"
17+ "time"
18+ )
19+
20+ func main () {
21+ var (
22+ mux = http .NewServeMux ()
23+ srv = & http.Server {Addr : ":8888" , Handler : mux }
24+ ctx , cxl = context .WithTimeout (context .Background (), 3 * time .Second )
25+ )
26+ defer cxl ()
27+
28+ mux .HandleFunc ("/hello" , func (w http.ResponseWriter , r * http.Request ) {
29+ query := r .URL .Query ()
30+ fmt .Println (query .Get ("message" ))
31+ })
32+
33+ go func () {
34+ srv .ListenAndServe ()
35+ }()
36+
37+ go func () {
38+ for {
39+ _ , err := http .Get ("http://localhost:8888/hello?message=world" )
40+ if err == nil {
41+ break
42+ }
43+ }
44+ }()
45+
46+ <- ctx .Done ()
47+ srv .Shutdown (ctx )
48+ }
You can’t perform that action at this time.
0 commit comments