Home > Backend Development > Golang > Why Do I Get 'package XXX is not in GOROOT' When Building My Go Project?

Why Do I Get 'package XXX is not in GOROOT' When Building My Go Project?

Mary-Kate Olsen
Release: 2024-12-17 09:47:25
Original
448 people have browsed it

Why Do I Get

"Package XXX is Not in GOROOT" When Building a 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:

  • Ensure a go.mod file exists at the project root, which designates the directory as a Go module.
  • Initialize the module using go mod init remote-repo.com/username/repository.
  • Specify the full package path when running commands, such as go COMMAND package_path/xxx. This prevents the compiler from assuming the package is in GOROOT.
  • Go uses the project workspace or current working directory for packages that are not part of the Go SDK.
  • To install an executable binary, use go install.
  • To compile an executable in the current directory, use go build.

Workflow:

  1. Initialize the Go module at the project root using go mod init.
  2. Run tests using go test -v ./... (recursively) or go test -v ./xxx (for a specific package).
  3. Compile and execute the package using go run ./... (recursively) or go run ./xxx (for a specific package).

Code Example for Version 1:

add.go:

package main

func addition(x int, y int) int {
    return x + y
}
Copy after login

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)
        }
    })

}
Copy after login

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)
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template