Skip to content

Commit 606710f

Browse files
authored
Merge pull request #562 from devlights/add-go-collective-example
2 parents 6451356 + 10ffad3 commit 606710f

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

.gitpod.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ tasks:
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

0 commit comments

Comments
 (0)