0

I'm new to go and I have a problem when I write packages. when importing the package I get the error:

 package mypackages/numbers is not in GOROOT (/usr/local/Cellar/go/1.16.5/libexec/src/mypackages/numbers)

this is the code structure from my $GOPATH/src:

.
├── github.com
├── gitlab.com
├── golang.org
├── learning
│   └── test
│       └── main.go
└── mypackages
    └── numbers
        └── numbers.go

learning/test/main.go content:

package main

import (
    "fmt"
    "mypackages/numbers"
)

func main() {
    fmt.Println(numbers.Even(6))
}

mypackages/numbers/numbers.go content:

package numbers

func Even(n uint) bool {
    if n%2 == 0 {
        return true
    }
    return false
}

The environment variables running go env:

GOCACHE="/Users/AminBSHR/Library/Caches/go-build"
GOENV="/Users/AminBSHR/Library/Application Support/go/env"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOMODCACHE="/Users/AminBSHR/go/pkg/mod"
GOPATH="/Users/AminBSHR/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.5/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.16.5/libexec/pkg/tool/darwin_amd64"
Amin Bashiri
  • 198
  • 1
  • 2
  • 16

1 Answers1

4

As you're new to Go, I recommend going through the following path, using official documentation pages:

  1. Read about properly installing Go for your platform.
  2. Read the getting started tutorial which tells you how to create a simple "hello world" module properly, and also how to install 3rd-party packages and use them in your code.

It should take you no more than 20 minutes to go through these steps, and it's almost certain that you'll be able to accomplish your goal by the end of the process. As a bonus, keep going through the Getting Started guide beyond the first page to learn how to create your own Go modules, use them from other modules, write tests, build your code into a binary, and more.

This is IMHO the minimal background required to even try writing Go programs; without going through these steps, you will lack crucial fundamental understanding and it will be hard to even understand SO answers.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412