Skip to content

Commit 3c7702e

Browse files
committed
Initial commit
0 parents  commit 3c7702e

File tree

14 files changed

+321
-0
lines changed

14 files changed

+321
-0
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/learning2.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# golang-learn
2+
3+
浅学一下golang

channel/lv0.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package channel
2+
3+
var x int64
4+
var endCh = make(chan int64)
5+
6+
func add(ch chan int64) {
7+
for i := 0; i < 50000; i++ {
8+
// 发送一个值 挂起自身 等待其他协程唤醒
9+
ch <- 1
10+
// 自增操作
11+
x++
12+
// 操作结束,挂起自身,唤醒其他协程
13+
<-ch
14+
}
15+
// 操作完成唤醒主协程
16+
endCh <- 1
17+
}
18+
19+
//func main() {
20+
// ch := make(chan int64)
21+
//
22+
// go add(ch)
23+
// go add(ch)
24+
//
25+
// // 唤醒其中一个协程
26+
// <-ch
27+
// // 挂起主协程
28+
// <-endCh
29+
//
30+
// fmt.Println(x)
31+
//}

channel/lv1.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package channel
2+
3+
import "fmt"
4+
5+
func count(ch chan int64, endCh chan<- int64) {
6+
for {
7+
count := <-ch
8+
if count == 100 {
9+
break
10+
}
11+
fmt.Println(count)
12+
ch <- count + 1
13+
}
14+
endCh <- 1
15+
}
16+
17+
//func main() {
18+
// ch := make(chan int64)
19+
// endCh := make(chan int64)
20+
// go count(ch, endCh)
21+
// go count(ch, endCh)
22+
// ch <- 0
23+
// <-endCh
24+
//}

channel/lv2.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package channel
2+
3+
//func main() {
4+
// file, err := os.Open("plan.txt")
5+
// if err != nil {
6+
// fmt.Println(err)
7+
// return
8+
// }
9+
// //_, err = file.Write([]byte("I’m not afraid of difficulties and insist on learning programming"))
10+
// //if err != nil {
11+
// // fmt.Println(err)
12+
// // return
13+
// //}
14+
// s := ""
15+
// buffer := make([]byte, 10)
16+
// for {
17+
// bytes, err := file.Read(buffer)
18+
//
19+
// if err != nil {
20+
// if err != io.EOF {
21+
// fmt.Println(err)
22+
// }
23+
//
24+
// break
25+
// }
26+
//
27+
// s += string(buffer[:bytes])
28+
// }
29+
// fmt.Println(s)
30+
//}

channel/lv3.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package channel
2+
3+
//func main() {
4+
// over := make(chan bool)
5+
// go func() {
6+
// for i := 0; i < 10; i++ {
7+
// fmt.Println(i)
8+
// if i == 9 {
9+
// over <- true
10+
// }
11+
// }
12+
// }()
13+
// <-over
14+
// fmt.Println("over!!!")
15+
//}

gin/login.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package gin_learn
2+
3+
import (
4+
"fmt"
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
var auth = func(c *gin.Context) {
9+
//获取cookie
10+
value, err := c.Cookie("gin_cookie")
11+
if err != nil {
12+
c.JSON(403, gin.H{
13+
"message": "认证失败,没有cookie",
14+
})
15+
//认证失败
16+
//终止的意思,也就是说执行该函数,会终止后面所有的该请求下的函数
17+
c.Abort()
18+
} else {
19+
//将获取到的cookie的值写入上下文
20+
c.Set("cookie", value)
21+
//这里随便介绍一下next方法
22+
//挂起继续向下走,然后执行完成下面的函数,会反过来最后执行该中间件
23+
c.Next()
24+
v, _ := c.Get("next")
25+
fmt.Println(v)
26+
}
27+
}
28+
29+
func InitLogin(r *gin.Engine) {
30+
r.POST("/login", func(c *gin.Context) {
31+
username := c.PostForm("username")
32+
password := c.PostForm("password")
33+
if username == "123" && password == "321" {
34+
//对着函数摁一下ctrl+b看看对应参数
35+
c.SetCookie("gin_cookie", username, 3600, "/", "", false, true)
36+
c.JSON(200, gin.H{
37+
"msg": "login successfully",
38+
})
39+
} else {
40+
c.JSON(403, gin.H{
41+
"message": "认证失败,账号密码错误",
42+
})
43+
}
44+
})
45+
}

gin/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package gin_learn
2+
3+
import "github.com/gin-gonic/gin"
4+
5+
func Run() {
6+
// Creates a gin router with default middleware:
7+
// logger and recovery (crash-free) middleware
8+
r := gin.Default()
9+
// 初始化登录模块
10+
InitLogin(r)
11+
//在中间放入鉴权中间件
12+
r.GET("/hello", auth, func(c *gin.Context) {
13+
cookie, _ := c.Get("cookie")
14+
str := cookie.(string)
15+
c.String(200, "hello world"+str)
16+
//测试next函数
17+
c.Set("next", "test next")
18+
})
19+
// By default, it serves on :8080 unless a
20+
// PORT environment variable was defined.
21+
err := r.Run()
22+
if err != nil {
23+
return
24+
}
25+
// router.Run(":3000") for a hard coded port
26+
}

go.mod

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module learning2
2+
3+
go 1.19
4+
5+
require (
6+
github.com/gin-contrib/sse v0.1.0 // indirect
7+
github.com/gin-gonic/gin v1.8.1 // indirect
8+
github.com/go-playground/locales v0.14.0 // indirect
9+
github.com/go-playground/universal-translator v0.18.0 // indirect
10+
github.com/go-playground/validator/v10 v10.11.0 // indirect
11+
github.com/goccy/go-json v0.9.11 // indirect
12+
github.com/json-iterator/go v1.1.12 // indirect
13+
github.com/leodido/go-urn v1.2.1 // indirect
14+
github.com/mattn/go-isatty v0.0.16 // indirect
15+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
16+
github.com/modern-go/reflect2 v1.0.2 // indirect
17+
github.com/pelletier/go-toml/v2 v2.0.3 // indirect
18+
github.com/ugorji/go/codec v1.2.7 // indirect
19+
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 // indirect
20+
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c // indirect
21+
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
22+
golang.org/x/text v0.3.7 // indirect
23+
google.golang.org/protobuf v1.28.1 // indirect
24+
gopkg.in/yaml.v2 v2.4.0 // indirect
25+
)

go.sum

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
5+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
6+
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
7+
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
8+
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
9+
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
10+
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
11+
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
12+
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
13+
github.com/go-playground/validator/v10 v10.11.0 h1:0W+xRM511GY47Yy3bZUbJVitCNg2BOGlCyvTqsp/xIw=
14+
github.com/go-playground/validator/v10 v10.11.0/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
15+
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
16+
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
17+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
18+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
19+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
20+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
21+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
22+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
23+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
24+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
25+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
26+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
27+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
28+
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
29+
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
30+
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
31+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
32+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
33+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
34+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
35+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
36+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
37+
github.com/pelletier/go-toml/v2 v2.0.3 h1:h9JoA60e1dVEOpp0PFwJSmt1Htu057NUq9/bUwaO61s=
38+
github.com/pelletier/go-toml/v2 v2.0.3/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
39+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
40+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
41+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
42+
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
43+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
44+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
45+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
46+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
47+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
48+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
49+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
50+
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
51+
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
52+
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
53+
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
54+
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
55+
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 h1:vJ2V3lFLg+bBhgroYuRfyN583UzVveQmIXjc8T/y3to=
56+
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
57+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
58+
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c h1:JVAXQ10yGGVbSyoer5VILysz6YKjdNT2bsvlayjqhes=
59+
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
60+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
61+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
62+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63+
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
64+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
65+
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 h1:TyKJRhyo17yWxOMCTHKWrc5rddHORMlnZ/j57umaUd8=
66+
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
67+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
68+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
69+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
70+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
71+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
72+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
73+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
74+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
75+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
76+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
77+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
78+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
79+
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
80+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
81+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
82+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
83+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
84+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

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

0 commit comments

Comments
 (0)