undefined

一、runtime.Gosched 函数解析

作用:让当前 goroutine 让出 CPU,好让其他的 goroutine 获得执行的机会。同时,当时的 goroutine 也会在未来的某个时间点继续运行

1
runtime.Gosched()

二、获取Go程序的汇编代码

1
2
3
4
5
6
7
8
9
1. 使用 go tool compile 生成汇编代码
go tool compile -N -l -S xxx.go

2. 使用 go tool objdump
首先编译程序:go tool compile -N -l xxx.go
使用 go tool objdump xxx.go 反汇编出代码;或者使用 go tool objdump -s Do xxx.go 反汇编特定的函数

3. 使用 go build -gcflags -S
go build -gcflags -S xxx.go 得到汇编代码

查看更多

undefined

gin框架

基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
func RouteCtroler(r *gin.Engine) {
/*
r.GET("/",func(c *gin.Context) {
c.String(http.StatusOK,"hello world")
})
*/

// API 参数
/*
r.GET("/user/:name/*action", func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
action = strings.Trim(action,"/")
c.String(http.StatusOK, name + " is " + action)
})
*/

// URL 参数
/*
r.GET("/user", func(c *gin.Context) {
name := c.DefaultQuery("name","zhangyi")
c.String(http.StatusOK, fmt.Sprintf("hello %s", name))
})
*/

// 获取 Query 参数
/*
r.GET("/users", func(c *gin.Context) {
name := c.Query("name")
role := c.DefaultQuery("role","teacher")
c.String(http.StatusOK, "%s is a %s", name, role)
})
*/

// 获取POST参数
/*
r.POST("/form", func(c *gin.Context) {
username := c.PostForm("username")
password := c.DefaultPostForm("password","1234")
c.JSON(http.StatusOK,gin.H{
"username": username,
"password": password,
})
})
*/
}

func RouteMapParam(r *gin.Engine) {
r.POST("/post", func(c *gin.Context) {
ids := c.QueryMap("names")
names := c.PostFormMap("names")
c.JSON(http.StatusOK, gin.H{
"ids": ids,
"names": names,
})
})
}

// 利用分组路由可以实现权限控制等等等
func GroupRoute(r *gin.Engine) {
defaultHandler := func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"path": c.FullPath(),
})
}
v1 := r.Group("/v1")
{
v1.GET("/posts",defaultHandler)
v1.GET("/series",defaultHandler)
}
v2 := r.Group("/v2")
{
v2.GET("/posts",defaultHandler)
v2.GET("/series",defaultHandler)
}
}

func main(){
r := gin.Default()
//RouteMapParam(r)
GroupRoute(r)

r.Run(":8000")
}

中间路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
func MiddleWare() gin.HandlerFunc {
return func(c *gin.Context) {
t := time.Now()
fmt.Println("middleWare start")
// 设置变量到Context的key中,可以通过Get()取
c.Set("request","middleWare")
// 执行函数
c.Next()
status := c.Writer.Status()
fmt.Println("middleWare end", status)
t2 := time.Since(t)
fmt.Println("time:", t2)
}
}

func main() {
r := gin.Default()
r.Use(MiddleWare())
{
r.GET("/ce", func(c *gin.Context) {
req,_ := c.Get("request")
fmt.Println("request:", req)
c.JSON(200, gin.H{
"request": req,
})
})
}
r.Run(":8000")
}
*/

func myTime(c *gin.Context) {
start := time.Now()
c.Next()
since := time.Since(start)
fmt.Println("程序用时: ", since)
}

func shopIndexHandler(c *gin.Context) {
time.Sleep(3*time.Second)
}

func shopHomeHandler(c *gin.Context) {
time.Sleep(2*time.Second)
}

func main(){
// 1.创建路由
// 默认使用了2个中间件 logger(), Recovery()
r := gin.Default()
r.Use(myTime)
shoppingGroup := r.Group("/shopping")
{
shoppingGroup.GET("/index", shopIndexHandler)
shoppingGroup.GET("/home", shopHomeHandler)
}
r.Run(":8000")
}

查看更多

undefined

Go程序测试分为:功能测试、基准测试(性能测试)、示例测试

  • 测试源码文件的主名称应该以被测源码文件的主名称为前导,并且必须以“_test”为后缀
  • 每个测试源码文件都必须至少包含一个测试函数。并且,从语法上讲,每个测试源码文件中,都可以包含用来做任何一类测试的测试函数,即使把这三类测试函数都塞进去也没有问题。

查看更多

undefined

获取文件描述符fd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
l, err := net.Listen("tcp", ":9091")
handleErr(err)

conn, err := l.Accept()
handleErr(err)

if s, ok := conn.(*net.TCPConn); !ok {
handleErr(errors.New("not tcp conn"))
} else {
f, err := s.File()
handleErr(err)

fmt.Println(int(f.fd()))
}

对于socket的参数

go语言会在调用 net.Listen 的时候将 socket()、bind()、listen() 这几步一次性做完,所以我们只能使用 net.ListenConfig 设置回调函数以控制中间过程。在回调函数中拿到原始的文件描述符后,我们可以调用 syscall.SetsockoptInt 设置 socket 选项,这与原始的 setsockopt 系统调用类似。

1
2
3
4
5
6
7
8
9
cfg := net.ListenConfig{
Control: func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEADDR, 1)
syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1)
})
},
}
tcp, err := cfg.Listen(context.Background(), "tcp", "127.0.0.1:1234")

undefined

type关键字

https://studygolang.com/articles/17179

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main

// 1. 定义结构体
type person struct {
name string
age int
}

func main(){
p := person{"hello",12}
p1 := person{"hello"}
fmt.Println(p.age,p.name)
fmt.Println(p1.name,p1.age)
}

// 2. 类型等价定义,相当于类型重命名
type name string

func main(){
var myname name = "zhangyi"
//l := []byte(myname)
fmt.Println(myname)
}

// 3. 结构体内嵌匿名成员
type person struct {
string
age int
}

func main(){
p := person{"nihao",12}
p2 := person{string:"world",age:18}
fmt.Println(p.string,p2.string)
}

// 4.接口类型定义
type Personer interface {
Run()
Name() string
}

type person struct {
name string
age int
}
func (person) Run(){
fmt.Println("run")
}
func (p person) Name() string{
return p.name
}

func main(){
var p Personer
fmt.Println(p)

p = person{"hello",12}
p.Run()
fmt.Println(p.Name())

var p2 person = p.(person)
fmt.Println(p2.age)
if p3,ok := p.(person); ok{
fmt.Println(ok)
fmt.Println(p3.age)
}
}

// 5.定义函数类型
type handler func(name string) int

func (h handler) add(name string) int{
return h(name) + 10
}