Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
daidai21 committed Jun 28, 2022
1 parent 085b90b commit f2bdf4a
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
9 changes: 9 additions & 0 deletions reuse/golang/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ reflect_rule_example:

range_channel_example:
go run range_channel_example.go

channel_select_1_example:
go run channel_select_1_example.go

channel_select_2_example:
go run channel_select_2_example.go

context_deadline_example:
go run context_deadline_example.go
42 changes: 42 additions & 0 deletions reuse/golang/src/channel_select_1_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

// select的default逻辑

import (
"sync"
"time"
)

var wg sync.WaitGroup

func main() {
wg.Add(1)
ch := make(chan string)

go func() {
defer wg.Done()

for _ = range [10]int{} {
time.Sleep(1 * time.Millisecond)
select {
case i := <-ch:
println(i)
default:
println("default")
}
}
}()

ch <- "1"
time.Sleep(1 * time.Millisecond)
ch <- "2"
time.Sleep(1 * time.Millisecond)
ch <- "3"
time.Sleep(1 * time.Millisecond)
ch <- "4"
time.Sleep(1 * time.Millisecond)
ch <- "5"

close(ch)
wg.Wait()
}
26 changes: 26 additions & 0 deletions reuse/golang/src/channel_select_2_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

// channel 阻塞消费

import (
"fmt"
)

func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)

// 阻塞等待
x, y := <-c, <-c // receive from c

fmt.Println(x, y, x+y)
}
3 changes: 3 additions & 0 deletions reuse/golang/src/channel_switch_select.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

// TODO
25 changes: 25 additions & 0 deletions reuse/golang/src/context_deadline_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

// context.WithDeadline() example

import (
"context"
"fmt"
"time"
)

func main() {
d := time.Now().Add(50 * time.Millisecond)

ctx, cancel := context.WithDeadline(context.Background(), d)

defer cancel()

select {
case <-time.After(1 * time.Second):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err())

}
}
22 changes: 22 additions & 0 deletions reuse/sql/reuse.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ WHERE name LIKE 'test%'
LIMIT 10
;

-- json get 通过key获取value
-- {\"test\":true}
SELECT json_col->'$.test'
FROM t_test
;


-- 捞出所有重复数量等于3的id
SELECT DISTINCT id
FROM t_test
GROUP BY id
HAVING COUNT(id) = 3
;


-- 今天从0点开始插入的数据行数
SELECT COUNT(*)
FROM `t_test`
WHERE gmt_create > DATE_FORMAT(CURDATE(),'%Y-%m-%d %H:%i:%s')
;


SHOW status LIKE 'Com_select%';
SHOW status LIKE 'Com_insert%';
SHOW status LIKE 'Com_update%';
Expand Down

0 comments on commit f2bdf4a

Please sign in to comment.