问题:
尝试构建 Go 项目时,错误消息“package XXX is not in GOROOT”可能
解决方案:
在较新版本的 Go(1.13 后)中,不再需要 GOPATH、GOBIN 等环境变量。相反:
工作流程:
版本代码示例1:
add.go:
package main func addition(x int, y int) int { return x + y }
add_ test.go:
package main import "testing" func TestAdd(t *testing.T) { t.Run("adding two positive numbers", func(t *testing.T) { sum := addition(2, 2) expected := 4 if sum != expected { t.Errorf("Expected %d; but got %d", expected, sum) } }) t.Run("adding two negative numbers", func(t *testing.T) { sum := addition(-3, -4) expected := -7 if sum != expected { t.Errorf("Expected %d; but got %d", expected, sum) } }) t.Run("adding one positive and one negative integer", func(t *testing.T) { sum := addition(1, -3) expected := -2 if sum != expected { t.Errorf("Expected %d; but got %d", expected, sum) } }) }
main.go:
package main import "fmt" func main() { var num1 int = 1 var num2 int = 2 sum := addition(num1, num2) product := multiplication(num1, num2) fmt.Printf("The sum of %d and %d is %d\n", num1, num2, sum) fmt.Printf("The multiplication of %d and %d is %d\n", num1, num2, product) }
以上是为什么我在构建 Go 项目时收到'package XXX is not in GOROOT”?的详细内容。更多信息请关注PHP中文网其他相关文章!