問題:
嘗試建置Go 專案時,錯誤訊息「 package XXX is not in GOROOT」可能
解:
在較新版本的 Go(1.13 後)中,不再需要 GOPATH、GOBIN 等環境變數。相反:
工作流程:
使用 go run ./...(遞歸)或 go run ./xxx(對於特定套件)編譯並執行套件。
版本程式碼範例1:
package main func addition(x int, y int) int { return x + y }
add.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) } }) }
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中文網其他相關文章!