Skip to content

Commit 847db6a

Browse files
committed
<实验><代码实验案例提交>
1 parent dca0e0f commit 847db6a

File tree

8 files changed

+185
-5
lines changed

8 files changed

+185
-5
lines changed

demo/nsq-demo/nsq_producer.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"github.com/labstack/echo"
5-
"net/http"
67
"github.com/nsqio/go-nsq"
78
"log"
8-
"fmt"
9-
"encoding/json"
9+
"net/http"
1010
)
1111

1212
type ProBody struct {

labs/default-selection/main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func main() {
9+
tick := time.Tick(100 * time.Millisecond)
10+
boom := time.After(500 * time.Millisecond)
11+
for {
12+
select {
13+
case <-tick:
14+
fmt.Println("tick.")
15+
case <-boom:
16+
fmt.Println("BOOM!")
17+
return
18+
default:
19+
fmt.Println(" .")
20+
time.Sleep(50 * time.Millisecond)
21+
}
22+
}
23+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func gen() chan int {
8+
out := make(chan int)
9+
fmt.Println("gen start")
10+
go func() {
11+
for i := 0; i < 100; i++ {
12+
out <- i
13+
}
14+
}()
15+
fmt.Println("gen end")
16+
return out
17+
}
18+
19+
func seq(input chan int) {
20+
for num := range input {
21+
fmt.Println(num)
22+
}
23+
}
24+
25+
func main() {
26+
in := gen()
27+
go seq(in)
28+
}

labs/goroutine-channel-url/main.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/astaxie/beego/httplib"
6+
)
7+
8+
func getUrlBody() chan string {
9+
str := make(chan string)
10+
fmt.Println("url start")
11+
go func() {
12+
fmt.Println("httplib start")
13+
urlStr, err := httplib.Get("http://httpbin.org/get").String()
14+
if err != nil {
15+
16+
}
17+
fmt.Println("httplib end")
18+
str <- urlStr
19+
}()
20+
fmt.Println("url end")
21+
return str
22+
}
23+
24+
func strPrint(input chan string) {
25+
for ss := range input {
26+
fmt.Println(ss)
27+
}
28+
}
29+
30+
func main() {
31+
str := getUrlBody()
32+
fmt.Println(str)
33+
go strPrint(str)
34+
select {}
35+
}

labs/goroutine/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package main
22

33
import (
44
"fmt"
5-
"time"
65
)
76

87
func say(s string) {
98
for i := 0; i < 5; i++ {
10-
time.Sleep(100 * time.Millisecond)
9+
//time.Sleep(100 * time.Millisecond)
1110
fmt.Println(s)
1211
}
1312
}

labs/goroutine/nogoroutine/main.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
// demo URL http://blog.teamtreehouse.com/goroutines-concurrency
10+
func generateKey() int {
11+
fmt.Println("Generating key")
12+
// Super-secret algorithm!
13+
keys := []int{3, 5, 7, 11}
14+
key := keys[rand.Intn(len(keys))]
15+
// It's kinda slow!
16+
time.Sleep(3 * time.Second)
17+
fmt.Println("Done generating")
18+
return key
19+
}
20+
21+
func main() {
22+
rand.Seed(time.Now().Unix())
23+
// Call generateKey 3 times.
24+
for i := 0; i < 3; i++ {
25+
fmt.Println(generateKey())
26+
}
27+
fmt.Println("All done!")
28+
}

labs/goroutine/usegoroutine/main.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
// Update this function to accept a channel parameter,
10+
// and remove the return value.
11+
func generateKey(channel chan int) {
12+
fmt.Println("Generating key")
13+
keys := []int{3, 5, 7, 11}
14+
key := keys[rand.Intn(len(keys))]
15+
time.Sleep(3 * time.Second)
16+
fmt.Println("Done generating")
17+
// Write the key to the channel instead of returning.
18+
channel <- key
19+
}
20+
21+
func main() {
22+
rand.Seed(time.Now().Unix())
23+
// Create a channel.
24+
channel := make(chan int)
25+
// Create 3 more goroutines.
26+
for i := 0; i < 3; i++ {
27+
go generateKey(channel)
28+
}
29+
// Read and print keys from the channel.
30+
// This also causes the program to wait until 3
31+
// keys have been read.
32+
for i := 0; i < 3; i++ {
33+
fmt.Println(<-channel)
34+
}
35+
//for i := range channel {
36+
// fmt.Println(i)
37+
//}
38+
fmt.Println("All done!")
39+
}

labs/select/main.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func fibonacci(c, quit chan int) {
6+
x, y := 0, 1
7+
for {
8+
select {
9+
case c <- x:
10+
x, y = y, x+y
11+
case <-quit:
12+
fmt.Println("quit")
13+
return
14+
}
15+
}
16+
}
17+
18+
func main() {
19+
c := make(chan int)
20+
quit := make(chan int)
21+
go func() {
22+
for i := 0; i < 10; i++ {
23+
fmt.Println(<-c)
24+
}
25+
quit <- 0
26+
}()
27+
fibonacci(c, quit)
28+
}

0 commit comments

Comments
 (0)