how to fix GOROOT problem in local packages in mac

huangapple go评论95阅读模式
英文:

how to fix GOROOT problem in local packages in mac

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习Go语言,写包的时候遇到了问题。在导入包时,我遇到了以下错误:

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

这是我$GOPATH/src目录下的代码结构:

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

learning/test/main.go 内容:

package main

import (
	"fmt"
	"mypackages/numbers"
)

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

mypackages/numbers/numbers.go 内容:

package numbers

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

运行 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"
英文:

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"

答案1

得分: 4

作为你的中文翻译,以下是翻译好的内容:

由于你刚开始学习Go语言,我建议你按照以下路径,使用官方文档页面进行学习:

  1. 阅读关于在你的平台上正确安装Go的内容。
  2. 阅读入门教程,该教程将告诉你如何正确创建一个简单的“hello world”模块,以及如何安装第三方包并在你的代码中使用它们。

完成这些步骤应该不会超过20分钟的时间,几乎可以确定你在学习过程结束时将能够实现你的目标。作为额外的奖励,继续阅读入门指南的后续页面,学习如何创建自己的Go模块,如何从其他模块中使用它们,编写测试,将你的代码构建为可执行文件等等。

这是我个人认为至少需要的背景知识,即使尝试编写Go程序也是如此;如果不按照这些步骤进行学习,你将缺乏关键的基础理解,甚至很难理解Stack Overflow上的答案。

英文:

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.

huangapple
  • 本文由 发表于 2021年6月25日 02:29:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/68121036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定