在Debian上编写和执行Golang测试的步骤如下:
首先,确保你已经在Debian系统上安装了Golang。如果还没有安装,可以通过以下命令安装:
sudo apt update
sudo apt install golang-go
在终端中运行以下命令,设置GOPATH和GOROOT环境变量:
echo "export GOROOT=/usr/lib/go" >> ~/.profile
echo "export GOPATH=$HOME/go" >> ~/.profile
echo "export PATH=$PATH:$GOROOT/bin:$GOPATH/bin" >> ~/.profile
source ~/.profile
在你的工作目录中创建一个新的Go项目文件夹,并进入该文件夹:
mkdir my_project
cd my_project
在项目文件夹中创建一个名为main.go的文件,编写你的Go代码。例如:
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func main() {
fmt.Println("Hello, World!")
}
接下来,创建一个名为main_test.go的文件,编写针对main.go中的add函数的测试:
package main
import "testing"
func TestAdd(t *testing.T) {
result := add(2, 3)
if result != 5 {
t.Errorf("Expected 5, but got %d", result)
}
}
在项目文件夹中,使用go test命令运行测试:
go test
如果测试通过,你将看到以下输出:
PASS
ok my_project 0.001s
如果有测试失败,你将看到详细的错误信息。
这就是在Debian上编写和执行Golang测试的方法。你可以根据需要创建更多的Go文件和测试文件,并使用go test命令运行它们。