Go在go/pkg中找不到包,而是在go/src中查找。

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

Go cannot find packages in go/pkg looking in go/src

问题

尝试使用GoLang和MQTT,但是Go似乎无法找到这些包。

当我尝试运行时,会出现以下错误:

go run foobar.go
foobar.go:9:2: no required module provides package github.com/eclipse/paho.mqtt.golang: go.mod file not found in current directory or any parent directory; see 'go help modules'

我设置了$GOPATH和$GOROOT以匹配go env。我甚至尝试回到旧的路径方法,通过设置GO111MODULE="off",但是结果出现了这个错误:

go run foobar.go
foobar.go:8:2: cannot find package "github.com/eclipse/paho.mqtt.golang" in any of:
	/usr/local/go/src/github.com/eclipse/paho.mqtt.golang (from $GOROOT)
	/Users/{user}/go/src/github.com/eclipse/paho.mqtt.golang (from $GOPATH)

我感到困惑,因为go get/install github.com/eclipse/paho.mqtt.golang似乎正在安装我的包,而所有我阅读的文档都指示了/src目录,那为什么我有/pkg目录呢?

英文:

trying to work with GoLang and MQTT but go seems unable to find the packages.

% go version
go version go1.16.7 darwin/amd64

foobar.go

package main

import (
	"fmt"
	"log"
	"os/exec"
	"time"
	mqtt "github.com/eclipse/paho.mqtt.golang"
)

when trying to run I would get the following error

go run foobar.go
foobar.go:9:2: no required module provides package github.com/eclipse/paho.mqtt.golang: go.mod file not found in current directory or any parent directory; see 'go help modules'
% go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/{user}/Library/Caches/go-build"
GOENV="/Users/{user}/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/{user}/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/{user}/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.7"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/rg/80kn_qfn0_91cv0_fb43_m5r0000gn/T/go-build2547135435=/tmp/go-build -gno-record-gcc-switches -fno-common"

I set $GOPATH and $GOROOT to match the go env. I even tried going back to the old path method by setting GO111MODULE="off", but that resulted in this error

% go run foobar.go
foobar.go:8:2: cannot find package "github.com/eclipse/paho.mqtt.golang" in any of:
	/usr/local/go/src/github.com/eclipse/paho.mqtt.golang (from $GOROOT)
	/Users/{user}/go/src/github.com/eclipse/paho.mqtt.golang (from $GOPATH)

I am confused because go get/install github.com/eclipse/paho.mqtt.golang appears to be installing of my packages are being installed under go/pkg/ while the above output and all the documentation I read says indicated the /src so why do I have the /pkg directory.

答案1

得分: 5

请使用模块。您不应该设置GOROOTGOPATH

我建议按照以下路径进行操作,使用官方文档页面:

  1. 阅读有关在您的平台上正确安装Go的内容。
  2. 阅读入门教程,该教程还告诉您如何安装第三方包并在代码中使用它们。

完成这些步骤应该不会超过20分钟,几乎可以肯定您将能够在过程结束时实现您的目标。作为奖励,继续阅读入门指南的第一页以后的内容,了解如何创建自己的Go模块,如何从其他模块中使用它们,编写测试,将代码build为二进制文件等等。

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

英文:

Please use modules. You should not be setting GOROOT or GOPATH.

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 also tells you 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年11月12日 07:47:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/69936311.html
匿名

发表评论

匿名网友

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

确定