Skip to content

Commit 0e6f07a

Browse files
committed
Select
1 parent e7f0040 commit 0e6f07a

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package examples
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"net/http"
10+
"time"
11+
)
12+
13+
// https://footba11.co/json/liveFeed
14+
// https://livescore-api.varzesh3.com/v1.0/livescore/today
15+
16+
// referer: https://www.varzesh3.com/
17+
18+
func SelectExample1() {
19+
resource1 := make(chan string)
20+
resource2 := make(chan string)
21+
var result = ""
22+
23+
go GetResponse(resource1, "https://footba11.co/json/liveFeed")
24+
go GetResponse(resource2, "https://livescore-api.varzesh3.com/v1.0/livescore/today")
25+
26+
select {
27+
case result = <-resource1:
28+
println("footba11 returned")
29+
case result = <-resource2:
30+
println("varzesh3 returned")
31+
case <-time.After(time.Second * 3):
32+
println("Timeout")
33+
default:
34+
println("Default")
35+
}
36+
println(result)
37+
PrintlnWithTime("End")
38+
}
39+
40+
func SelectExample2() {
41+
resource1 := make(chan string)
42+
resource2 := make(chan string)
43+
var result string
44+
45+
go GetResponse(resource1, "https://footba11.co/json/liveFeed")
46+
go GetResponse(resource2, "https://livescore-api.varzesh3.com/v1.0/livescore/today")
47+
48+
for {
49+
select {
50+
case result = <-resource1:
51+
println("footba11 returned")
52+
println(result)
53+
return
54+
case result = <-resource2:
55+
println("varzesh3 returned")
56+
println(result)
57+
return
58+
default:
59+
println("Default")
60+
}
61+
}
62+
63+
}
64+
65+
func GetResponse(content chan<- string, url string) {
66+
// if url == "https://livescore-api.varzesh3.com/v1.0/livescore/today" {
67+
// time.Sleep(time.Millisecond * 50)
68+
// }
69+
// time.Sleep(time.Second * 3)
70+
client := http.Client{}
71+
72+
request, err := http.NewRequest("GET", url, nil)
73+
74+
ctx := context.Background()
75+
76+
ctx, cancel := context.WithTimeout(ctx, time.Millisecond*10000)
77+
defer cancel()
78+
request = request.WithContext(ctx)
79+
80+
if err != nil {
81+
panic(err)
82+
}
83+
84+
request.Header = http.Header{}
85+
request.Header.Add("referer", "https://www.varzesh3.com/")
86+
87+
response, err := client.Do(request)
88+
89+
if err != nil {
90+
panic(err)
91+
}
92+
93+
defer response.Body.Close()
94+
95+
responseBody, err := ioutil.ReadAll(response.Body)
96+
97+
if err != nil {
98+
panic(err)
99+
}
100+
101+
destination := &bytes.Buffer{}
102+
103+
if err = json.Indent(destination, responseBody, "", " "); err != nil {
104+
panic(err)
105+
}
106+
PrintlnWithTime("Before set content")
107+
content <- destination.String()
108+
PrintlnWithTime("After set content")
109+
110+
}
111+
112+
func PrintlnWithTime(args ...any) {
113+
fmt.Printf("Time: %s, %v\n", time.Now().Format(time.RFC3339Nano), args)
114+
}

15-Concurrency/12-Select/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module select
2+
3+
go 1.18

15-Concurrency/12-Select/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "select/examples"
4+
5+
func main() {
6+
examples.SelectExample2()
7+
}

0 commit comments

Comments
 (0)