Home >Backend Development >Golang >Why Do I Get 'package XXX is not in GOROOT' When Building My Go Project?
Issue:
When attempting to build a Go project, the error message "package XXX is not in GOROOT" may appear.
Solution:
In newer versions of Go (post 1.13), environment variables like GOPATH, GOBIN, etc., are no longer necessary. Instead:
Workflow:
Code Example for Version 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) }
The above is the detailed content of Why Do I Get 'package XXX is not in GOROOT' When Building My Go Project?. For more information, please follow other related articles on the PHP Chinese website!