Skip to content

Commit 12d6740

Browse files
committed
init
1 parent 8fe5aaa commit 12d6740

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+6292
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
go.sum

_/test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type ITest interface {
6+
Add() string
7+
Bb() string
8+
}
9+
10+
type A struct {
11+
12+
}
13+
14+
func (a *A)Add() string {
15+
return "A"
16+
}
17+
18+
type B struct {
19+
20+
}
21+
22+
func (b *B)Add() string {
23+
return "bb"
24+
}
25+
26+
func (b *B)Bb() string {
27+
return "bba"
28+
}
29+
30+
31+
var _ ITest = (*A)(nil)
32+
var _ ITest = (*B)(nil)
33+
34+
var _ ITest = &A{}
35+
var _ ITest = &B{}
36+
37+
func main() {
38+
a := &A{}
39+
as := a.Add()
40+
fmt.Println(as)
41+
}

buffer/test.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/binary"
6+
"fmt"
7+
)
8+
9+
func main() {
10+
//newBuffer 整形转换成字节
11+
var n int = 10000
12+
intToBytes := IntToBytes(n)
13+
fmt.Println("==========int to bytes========")
14+
fmt.Println(intToBytes)
15+
//NewBufferString
16+
TestBufferString()
17+
//write
18+
BufferWrite()
19+
//WriteString
20+
BufferWriteString()
21+
//WriteByte
22+
BufferWriteByte()
23+
//WriteRune
24+
BufferWriteRune()
25+
26+
}
27+
28+
29+
func IntToBytes(n int) []byte {
30+
x := int32(n)
31+
//创建一个内容是[]byte的slice的缓冲器
32+
//与bytes.NewBufferString("")等效
33+
bytesBuffer := bytes.NewBuffer([]byte{})
34+
binary.Write(bytesBuffer, binary.BigEndian, x)
35+
return bytesBuffer.Bytes()
36+
}
37+
38+
func TestBufferString(){
39+
buf1:=bytes.NewBufferString("swift")
40+
buf2:=bytes.NewBuffer([]byte("swift"))
41+
buf3:=bytes.NewBuffer([]byte{'s','w','i','f','t'})
42+
fmt.Println("===========以下buf1,buf2,buf3等效=========")
43+
fmt.Println("buf1:", buf1)
44+
fmt.Println("buf2:", buf2)
45+
fmt.Println("buf3:", buf3)
46+
fmt.Println("===========以下创建空的缓冲器等效=========")
47+
buf4:=bytes.NewBufferString("")
48+
buf5:=bytes.NewBuffer([]byte{})
49+
fmt.Println("buf4:", buf4)
50+
fmt.Println("buf5:", buf5)
51+
}
52+
53+
func BufferWrite(){
54+
fmt.Println("===========以下通过Write把swift写入Learning缓冲器尾部=========")
55+
newBytes := []byte("swift")
56+
//创建一个内容Learning的缓冲器
57+
buf := bytes.NewBuffer([]byte("Learning"))
58+
//打印为Learning
59+
fmt.Println(buf.String())
60+
//将newBytes这个slice写到buf的尾部
61+
buf.Write(newBytes)
62+
fmt.Println(buf.String())
63+
}
64+
65+
func BufferWriteString(){
66+
fmt.Println("===========以下通过Write把swift写入Learning缓冲器尾部=========")
67+
newString := "swift"
68+
//创建一个string内容Learning的缓冲器
69+
buf := bytes.NewBufferString("Learning")
70+
//打印为Learning
71+
fmt.Println(buf.String())
72+
//将newString这个string写到buf的尾部
73+
buf.WriteString(newString)
74+
fmt.Println(buf.String())
75+
}
76+
77+
func BufferWriteByte(){
78+
fmt.Println("===========以下通过WriteByte把swift写入Learning缓冲器尾部=========")
79+
var newByte byte = '!'
80+
//创建一个string内容Learning的缓冲器
81+
buf := bytes.NewBufferString("Learning")
82+
//打印为Learning
83+
fmt.Println(buf.String())
84+
//将newString这个string写到buf的尾部
85+
buf.WriteByte(newByte)
86+
fmt.Println(buf.String())
87+
}
88+
89+
func BufferWriteRune(){
90+
fmt.Println("===========以下通过WriteRune把\"\"写入Learning缓冲器尾部=========")
91+
var newRune = '好'
92+
//创建一个string内容Learning的缓冲器
93+
buf := bytes.NewBufferString("Learning")
94+
//打印为Learning
95+
fmt.Println(buf.String())
96+
//将newString这个string写到buf的尾部
97+
buf.WriteRune(newRune)
98+
fmt.Println(buf.String())
99+
}

c/test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
/*
4+
#include <stdio.h>
5+
int Add(int a,int b) {
6+
return a+b;
7+
}
8+
*/
9+
import "C"
10+
11+
import "fmt"
12+
13+
func main() {
14+
fmt.Println(C.Add(1,2))
15+
}

context/cancel.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
)
8+
9+
func main() {
10+
// 手动取消
11+
a := 1
12+
b := 2
13+
ctx, cancel := context.WithCancel(context.Background())
14+
go func() {
15+
time.Sleep(2 * time.Second)
16+
cancel() // 在调用处主动取消
17+
}()
18+
res := Add(ctx, 1, 2)
19+
fmt.Printf("Compute: %d+%d, result: %d\n", a, b, res)
20+
}

context/test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
)
8+
9+
// 模拟一个最小执行时间的阻塞函数
10+
func inc(a int) int {
11+
res := a + 1 // 虽然我只做了一次简单的 +1 的运算,
12+
time.Sleep(1 * time.Second) // 但是由于我的机器指令集中没有这条指令,
13+
// 所以在我执行了 1000000000 条机器指令, 续了 1s 之后, 我才终于得到结果。B)
14+
return res
15+
}
16+
17+
// 向外部提供的阻塞接口
18+
// 计算 a + b, 注意 a, b 均不能为负
19+
// 如果计算被中断, 则返回 -1
20+
func Add(ctx context.Context, a, b int) int {
21+
res := 0
22+
for i := 0; i < a; i++ {
23+
res = inc(res)
24+
select {
25+
case <-ctx.Done():
26+
return -1
27+
default:
28+
}
29+
}
30+
for i := 0; i < b; i++ {
31+
res = inc(res)
32+
select {
33+
case <-ctx.Done():
34+
return -1
35+
default:
36+
}
37+
}
38+
return res
39+
}
40+
41+
func main() {
42+
{
43+
// 使用开放的 API 计算 a+b
44+
a := 1
45+
b := 2
46+
timeout := 2 * time.Second
47+
ctx, _ := context.WithTimeout(context.Background(), timeout)
48+
res := Add(ctx, a, b)
49+
fmt.Printf("Compute: %d+%d, result: %d\n", a, b, res)
50+
}
51+
{
52+
// 手动取消
53+
a := 5
54+
b := 2
55+
ctx, cancel := context.WithCancel(context.Background())
56+
go func() {
57+
time.Sleep(2 * time.Microsecond)
58+
cancel() // 在调用处主动取消
59+
}()
60+
res := Add(ctx, a, b)
61+
fmt.Printf("Compute2: %d+%d, result: %d\n", a, b, res)
62+
}
63+
}

context/test1.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
ctx, cancel := context.WithCancel(context.Background())
10+
11+
go func() {
12+
// ...
13+
// 你的逻辑
14+
// ...
15+
fmt.Println("1")
16+
select {
17+
case <-ctx.Done():
18+
}
19+
}()
20+
fmt.Println("aa")
21+
cancel()
22+
fmt.Println("2")
23+
}

context/test2.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
)
8+
9+
func main() {
10+
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
11+
defer cancel()
12+
go task(ctx)
13+
time.Sleep(time.Second * 10)
14+
}
15+
16+
func task(ctx context.Context) {
17+
ch := make(chan struct{}, 0)
18+
go func() {
19+
// 模拟4秒耗时任务
20+
time.Sleep(time.Second * 4)
21+
ch <- struct{}{}
22+
}()
23+
select {
24+
case <-ch:
25+
fmt.Println("done")
26+
case <-ctx.Done():
27+
fmt.Println("timeout")
28+
}
29+
}

context/test3.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
type favContextKey string
10+
11+
f := func(ctx context.Context, k favContextKey) {
12+
if v := ctx.Value(k); v != nil {
13+
fmt.Println("found value:", v)
14+
return
15+
}
16+
fmt.Println("key not found:", k)
17+
}
18+
19+
k := favContextKey("language")
20+
ctx := context.WithValue(context.Background(), k, "Go")
21+
22+
f(ctx, k)
23+
f(ctx, favContextKey("color"))
24+
}

context/test4.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
)
8+
9+
func monitor(ctx context.Context, number int) {
10+
for {
11+
select {
12+
// 其实可以写成 case <- ctx.Done()
13+
// 这里仅是为了让你看到 Done 返回的内容
14+
case v :=<- ctx.Done():
15+
fmt.Printf("监控器%v,接收到通道值为:%v,监控结束。\n", number,v)
16+
return
17+
default:
18+
fmt.Printf("监控器%v,正在监控中...\n", number)
19+
time.Sleep(2 * time.Second)
20+
}
21+
}
22+
}
23+
24+
func main() {
25+
ctx, cancel := context.WithCancel(context.Background())
26+
27+
for i :=1 ; i <= 5; i++ {
28+
go monitor(ctx, i)
29+
}
30+
31+
time.Sleep( 1 * time.Second)
32+
// 关闭所有 goroutine
33+
cancel()
34+
35+
// 等待5s,若此时屏幕没有输出 <正在监控中> 就说明所有的goroutine都已经关闭
36+
time.Sleep( 5 * time.Second)
37+
38+
fmt.Println("主程序退出!!")
39+
40+
}

cron/test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"github.com/robfig/cron"
5+
"log"
6+
)
7+
8+
func main() {
9+
i := 0
10+
c := cron.New()
11+
spec := "*/5 * * * * ?"
12+
c.AddFunc(spec, func() {
13+
i++
14+
log.Println("cron running :", i)
15+
})
16+
c.Start()
17+
select {
18+
19+
}
20+
}

0 commit comments

Comments
 (0)