开发工具

Go Delve

介绍 #

Delve 是 Go 编程语言的调试器。

原理 #

TODO

在 vscode 中使用 #

request 设置为 attach 并且 processId 设置为 0, 这样就会每次执行

在 go 项目根目录下设置 .vscode/launch.json 文件如下:

  • "type": "go": 配置类型为 go
  • "request": "attach": 这里配置为 attach 到一个已经运行的进程
  • "mode": "local": 相当于 attach 到本地进程
  • "processId": 0: 相当于没指定 pid, 每次启动 debug 会让选择进程
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Process",
            "type": "go",
            "request": "attach",
            "mode": "local",
            "processId": 0
        }
    ]
}

参考 #

Go Testing

介绍 #

testing 包为 Go 包自动化测试提供支持,它与 go test 命令一起使用,该命令可以自动执行下面这种格式的函数。

func TestXxx(*testing.T)

其中函数名中的 Xxx 不能以小写字母开头。

在函数中,使用 Error, Fail 或相关的函数表示失败。

要编写测试函数,需要县创建一个以 _test.go 结尾的文件,该文件会被排除在常规包构建之外,但将在运行 go test 命令时被包含在内。

一般将 _test.go 文件创建在要被测试的函数的同包下面

测试函数示例 #

比如这里我要验证 interfacenil 是否相等

// test/interface_test/interface_test.go
package interface_test

import "testing"

func TestInterfaceNil(t *testing.T) {
	var a interface{}
	var b = (*int)(nil)
	a = b
	if a == nil {
		t.Fatal("a == nil")
	}
}

go test 参数 #

有一些常用参数,可以通过 go help testgo help testflag 查看。

...