Go构建:"找不到包"(即使设置了GOPATH)

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

Go build: "Cannot find package" (even though GOPATH is set)

问题

尽管我已经正确设置了GOPATH,但我仍然无法让"go build"或"go run"找到我的自定义包。我做错了什么?

$ echo $GOROOT
/usr/local/go

$ echo $GOPATH
/home/mitchell/go

$ cat ~/main.go
package main
import "foobar"
func main() { }

$ cat /home/mitchell/go/src/foobar.go
package foobar

$ go build main.go
main.go:3:8: import "foobar": 找不到包
英文:

Even though I have GOPATH properly set, I still can't get "go build" or "go run" to find my own packages. What am I doing wrong?

$ echo $GOROOT
/usr/local/go

$ echo $GOPATH
/home/mitchell/go

$ cat ~/main.go
package main
import "foobar"
func main() { }

$ cat /home/mitchell/go/src/foobar.go
package foobar

$ go build main.go
main.go:3:8: import "foobar": cannot find package

答案1

得分: 189

它无法工作,因为您的foobar.go源文件不在名为foobar的目录中。go buildgo install尝试匹配目录,而不是源文件。

  1. $GOPATH设置为有效的目录,例如export GOPATH="$HOME/go"
  2. foobar.go移动到$GOPATH/src/foobar/foobar.go,然后构建应该正常工作。

额外推荐的步骤:

  1. 通过PATH="$GOPATH/bin:$PATH"$GOPATH/bin添加到您的$PATH
  2. main.go移动到$GOPATH/src的子文件夹中,例如$GOPATH/src/test
  3. go install test现在应该在$GOPATH/bin中创建一个可执行文件,可以通过在终端中键入test来调用。
英文:

It does not work because your foobar.go source file is not in a directory called foobar. go build and go install try to match directories, not source files.

  1. Set $GOPATH to a valid directory, e.g. export GOPATH="$HOME/go"
  2. Move foobar.go to $GOPATH/src/foobar/foobar.go and building should work just fine.

Additional recommended steps:

  1. Add $GOPATH/bin to your $PATH by: PATH="$GOPATH/bin:$PATH"
  2. Move main.go to a subfolder of $GOPATH/src, e.g. $GOPATH/src/test
  3. go install test should now create an executable in $GOPATH/bin that can be called by typing test into your terminal.

答案2

得分: 46

尽管接受的答案仍然正确,关于需要将目录与包名匹配,但您确实需要迁移到使用Go模块而不是使用GOPATH。遇到此问题的新用户可能会对使用GOPATH的提及感到困惑(就像我一样),这些提及现在已经过时。因此,我将尝试解决此问题并提供在使用Go模块时防止此问题的指导。

如果您已经熟悉Go模块并遇到此问题,请跳到下面更具体的部分,其中涵盖了一些容易忽视或忘记的Go约定。

这个指南介绍了Go模块:https://golang.org/doc/code.html

使用Go模块进行项目组织

一旦您迁移到Go模块,如上述文章所述,按照以下方式组织项目代码:

一个存储库包含一个或多个模块。模块是一组相关的Go包,它们一起发布。Go存储库通常只包含一个模块,位于存储库的根目录。名为go.mod的文件在那里声明了模块路径:模块内所有包的导入路径前缀。该模块包含其go.mod文件所在目录中的包,以及该目录的子目录,直到包含另一个go.mod文件的下一个子目录(如果有)。

每个模块的路径不仅用作其包的导入路径前缀,还指示go命令在哪里下载它。例如,为了下载模块golang.org/x/tools,go命令将查找由https://golang.org/x/tools指示的存储库(在此处有更详细的描述)。

导入路径是用于导入包的字符串。包的导入路径是其模块路径与模块内子目录的组合。例如,模块github.com/google/go-cmp包含目录cmp/中的一个包。该包的导入路径是github.com/google/go-cmp/cmp。标准库中的包没有模块路径前缀。

您可以像这样初始化您的模块:

$ go mod init github.com/mitchell/foo-app

您的代码不需要位于github.com上才能构建。但是,最好的做法是将模块结构化,就好像它们最终将被发布一样。

了解尝试获取包时会发生什么

这里有一篇很棒的文章,讲述了尝试获取包或模块时会发生什么:https://medium.com/rungo/anatomy-of-modules-in-go-c8274d215c16
它讨论了包存储的位置,并将帮助您理解如果您已经使用Go模块,为什么可能会遇到此错误。

确保导入的函数已导出

请注意,如果您无法从另一个文件中访问函数,您需要确保已导出该函数。如我提供的第一个链接中所述,函数必须以大写字母开头才能导出并可供其他包导入。

目录的名称

另一个关键细节(如接受的答案中所提到的)是目录的名称定义了包的名称。(您的包名需要与其目录名匹配。)您可以在这里看到示例:https://medium.com/rungo/everything-you-need-to-know-about-packages-in-go-b8bac62b74cc
也就是说,包含您的main方法(即应用程序的入口点)的文件在某种程度上不需要满足此要求。

例如,当我使用以下结构时,我在使用utils中的代码导入到我的main包时遇到了问题:

/my-app
├── go.mod
├── /src
├── main.go
└── /utils
└── utils.go

我无法将utils中的代码导入到我的main包中。

然而,一旦我将main.go放入自己的子目录中,如下所示,我的导入就正常工作了:

/my-app
├── go.mod
├── /src
├── /app
| └── main.go
└── /utils
└── utils.go

在这个例子中,我的go.mod文件如下所示:

module git.mydomain.com/path/to/repo/my-app

go 1.14

当我在添加对utils.MyFunction()的引用后保存main.go时,我的IDE会自动引入对我的包的引用,如下所示:

import "git.mydomain.com/path/to/repo/my-app/src/my-app"

(我正在使用带有Golang扩展的VS Code。)

请注意,导入路径包括到包的子目录的路径。

处理私有存储库

如果代码是私有存储库的一部分,您需要运行git命令以启用访问。否则,您可能会遇到其他错误。本文介绍了如何在私有Github、BitBucket和GitLab存储库中执行此操作:https://medium.com/cloud-native-the-gathering/go-modules-with-private-git-repositories-dfe795068db4
这个问题也在这里讨论:https://stackoverflow.com/questions/27500861/whats-the-proper-way-to-go-get-a-private-repository

英文:

Although the accepted answer is still correct about needing to match directories with package names, you really need to migrate to using Go modules instead of using GOPATH. New users who encounter this problem may be confused about the mentions of using GOPATH (as was I), which are now outdated. So, I will try to clear up this issue and provide guidance associated with preventing this issue when using Go modules.

If you're already familiar with Go modules and are experiencing this issue, skip down to my more specific sections below that cover some of the Go conventions that are easy to overlook or forget.

This guide teaches about Go modules: https://golang.org/doc/code.html

Project organization with Go modules

Once you migrate to Go modules, as mentioned in that article, organize the project code as described:

> A repository contains one or more modules. A module is a collection of
> related Go packages that are released together. A Go repository
> typically contains only one module, located at the root of the
> repository. A file named go.mod there declares the module path: the
> import path prefix for all packages within the module. The module
> contains the packages in the directory containing its go.mod file as
> well as subdirectories of that directory, up to the next subdirectory
> containing another go.mod file (if any).
>
> Each module's path not only serves as an import path prefix for its
> packages, but also indicates where the go command should look to
> download it. For example, in order to download the module
> golang.org/x/tools, the go command would consult the repository
> indicated by https://golang.org/x/tools (described more here).
>
> An import path is a string used to import a package. A package's
> import path is its module path joined with its subdirectory within the
> module. For example, the module github.com/google/go-cmp contains a
> package in the directory cmp/. That package's import path is
> github.com/google/go-cmp/cmp. Packages in the standard library do not
> have a module path prefix.

You can initialize your module like this:

$ go mod init github.com/mitchell/foo-app

Your code doesn't need to be located on github.com for it to build. However, it's a best practice to structure your modules as if they will eventually be published.

Understanding what happens when trying to get a package

There's a great article here that talks about what happens when you try to get a package or module: https://medium.com/rungo/anatomy-of-modules-in-go-c8274d215c16
It discusses where the package is stored and will help you understand why you might be getting this error if you're already using Go modules.

Ensure the imported function has been exported

Note that if you're having trouble accessing a function from another file, you need to ensure that you've exported your function. As described in the first link I provided, a function must begin with an upper-case letter to be exported and made available for importing into other packages.

Names of directories

Another critical detail (as was mentioned in the accepted answer) is that names of directories are what define the names of your packages. (Your package names need to match their directory names.) You can see examples of this here: https://medium.com/rungo/everything-you-need-to-know-about-packages-in-go-b8bac62b74cc
With that said, the file containing your main method (i.e., the entry point of your application) is sort of exempt from this requirement.

As an example, I had problems with my imports when using a structure like this:

/my-app
├── go.mod
├── /src
   ├── main.go
   └── /utils
      └── utils.go

I was unable to import the code in utils into my main package.

However, once I put main.go into its own subdirectory, as shown below, my imports worked just fine:

/my-app
├── go.mod
├── /src
   ├── /app
   |  └── main.go
   └── /utils
      └── utils.go

In that example, my go.mod file looks like this:

module git.mydomain.com/path/to/repo/my-app

go 1.14

When I saved main.go after adding a reference to utils.MyFunction(), my IDE automatically pulled in the reference to my package like this:

import "git.mydomain.com/path/to/repo/my-app/src/my-app"

(I'm using VS Code with the Golang extension.)

Notice that the import path included the subdirectory to the package.

Dealing with a private repo

If the code is part of a private repo, you need to run a git command to enable access. Otherwise, you can encounter other errors This article mentions how to do that for private Github, BitBucket, and GitLab repos: https://medium.com/cloud-native-the-gathering/go-modules-with-private-git-repositories-dfe795068db4
This issue is also discussed here: https://stackoverflow.com/questions/27500861/whats-the-proper-way-to-go-get-a-private-repository

答案3

得分: 20

我通过将我的go环境变量GO111MODULE设置为off来解决了这个问题。

go env -w GO111MODULE=off

注意:设置GO111MODULE=off将关闭最新的GO Modules功能。

参考:为什么GO111MODULE无处不在,以及关于Go Modules的一切(更新为Go 1.17)
> Go 1.16中的GO111MODULE
>
> 从Go 1.16开始,默认行为是GO111MODULE=on,这意味着如果你想继续使用旧的GOPATH方式,你将不得不强制Go不使用Go Modules功能:
>
> export GO111MODULE=off

英文:

I solved this problem by set my go env GO111MODULE to off

go env -w  GO111MODULE=off

Note: setting GO111MODULE=off will turn off the latest GO Modules feature.

Reference: Why is GO111MODULE everywhere, and everything about Go Modules (updated with Go 1.17)
> GO111MODULE with Go 1.16
>
> As of Go 1.16, the default behavior is GO111MODULE=on, meaning that if
> you want to keep using the old GOPATH way, you will have to force Go
> not to use the Go Modules feature:
>
> export GO111MODULE=off

答案4

得分: 13

在最近的Go版本1.14及以后,我们在构建或运行之前必须执行go mod vendor,因为默认情况下Go会在命令后面添加-mod=vendor。因此,在执行go mod vendor之后,如果我们尝试构建,就不会遇到这个问题。

英文:

In the recent go versions from 1.14 onwards, we have to do go mod vendor before building or running, since by default go appends -mod=vendor to the go commands.
So after doing go mod vendor, if we try to build, we won't face this issue.

答案5

得分: 10

编辑:由于你指的是GOPATH,请参见fasmat答案(已赞同)

如“如何让Go找到我的包?”中所述,您需要将包xxx放在目录xxx中。

请参阅Go语言规范

package math

> 一组具有相同PackageName的文件形成了一个包的实现。
实现可能要求包的所有源文件位于同一个目录中。

代码组织中提到:

> 在构建导入“widget”包的程序时,go命令会在Go根目录中查找src/pkg/widget,然后(如果在那里找不到包源代码)会在每个工作区中查找**src/widget**。

(“工作区”是您的GOPATH中的路径条目:该变量可以引用多个路径,用于src,bin,pkg


(原始答案)

您还应将GOPATH设置为~/go,而不是GOROOT,如“如何编写Go代码”中所示。

> Go路径用于解析导入语句。它由go/build包实现并记录在文档中。
>
> GOPATH环境变量列出了查找Go代码的位置。
在Unix上,该值是一个以冒号分隔的字符串。
在Windows上,该值是一个以分号分隔的字符串。
在Plan 9上,该值是一个列表。

这与GOROOT不同:

> Go二进制发行版假定它们将被安装在/usr/local/go(或Windows下的c:\Go)中,但也可以将它们安装在其他位置。
如果这样做,使用Go工具时需要将GOROOT环境变量设置为该目录。

英文:

Edit: since you meant GOPATH, see fasmat's answer (upvoted)

As mentioned in "How do I make go find my package?", you need to put a package xxx in a directory xxx.

See the Go language spec:

package math

> A set of files sharing the same PackageName form the implementation of a package.
An implementation may require that all source files for a package inhabit the same directory.

The Code organization mentions:

> When building a program that imports the package "widget" the go command looks for src/pkg/widget inside the Go root, and then—if the package source isn't found there—it searches for src/widget inside each workspace in order.

(a "workspace" is a path entry in your GOPATH: that variable can reference multiple paths for your 'src, bin, pkg' to be)


(Original answer)

You also should set GOPATH to ~/go, not GOROOT, as illustrated in "How to Write Go Code".

> The Go path is used to resolve import statements. It is implemented by and documented in the go/build package.
>
> The GOPATH environment variable lists places to look for Go code.
On Unix, the value is a colon-separated string.
On Windows, the value is a semicolon-separated string.
On Plan 9, the value is a list.

That is different from GOROOT:

> The Go binary distributions assume they will be installed in /usr/local/go (or c:\Go under Windows), but it is possible to install them in a different location.
If you do this, you will need to set the GOROOT environment variable to that directory when using the Go tools.

答案6

得分: 3

TL;DR: 遵循Go的惯例!(吃一堑,长一智),检查旧的Go版本并删除它们。安装最新版本。

对我来说解决方案是不同的。我在一个共享的Linux服务器上工作,多次验证了我的GOPATH和其他环境变量,但仍然无法工作。我遇到了几个错误,包括“找不到包”和“无法识别的导入路径”。尝试按照golang.org上的这个解决方案(包括卸载部分)仍然遇到问题。

花了一些时间才意识到还有一个未卸载的旧版本(运行go version然后再运行which go... 哎呀)导致我找到了这个问题并最终解决了。

英文:

TL;DR: Follow Go conventions! (lesson learned the hard way), check for old go versions and remove them. Install latest.

For me the solution was different. I worked on a shared Linux server and after verifying my GOPATH and other environment variables several times it still didn't work. I encountered several errors including 'Cannot find package' and 'unrecognized import path'. After trying to reinstall with this solution by the instructions on golang.org (including the uninstall part) still encountered problems.

Took me some time to realize that there's still an old version that hasn't been uninstalled (running go version then which go again... DAHH) which got me to this question and finally solved.

答案7

得分: 3

运行 go env -w GO111MODULE=auto 对我有用

英文:

Running go env -w GO111MODULE=auto worked for me

答案8

得分: 2

Without editing GOPATH or anything, in my case just worked the following:


/app
├── main.go
├── /utils
    └── utils.go

Import packages where needed. This can be unintuitive, because it isn't relative to the app path. You need to add the app in the package path too:

main.go:

package main

import(
   "app/util"  
)

Being in app directory, run:

go mod init app

go get <package/xxx>

go build main.go / go run main.go

You should be good to go.


GOPATH = /home/go

appPath = /home/projects/app

Create a proper go.mod and go.sum with go mod init app (delete old before)

After that resolve all dependencies like missing packages with go get github.com/example/package.

英文:

Without editing GOPATH or anything, in my case just worked the following:


/app
├── main.go
├── /utils
    └── utils.go

Import packages where needed. This can be unintuitive, because it isn't relative to the app path. You need to add the app in the package path too:

main.go:

package main

import(
   &quot;app/util&quot;  
)

Being in app directory, run:

go mod init app

go get &lt;package/xxx&gt;

go build main.go / go run main.go

You should be good to go.


GOPATH = /home/go

appPath = /home/projects/app

Create a proper go.mod and go.sum with go mod init app (delete old before)

After that resolve all dependencies like missing packages with go get github.com/example/package.

答案9

得分: 2

以简单的话来说,即使使用GO111MODULE=on,您也可以通过以下导入语法解决导入问题:

import <your_module_name>/<package_name>;

your_module_name -> 模块名称,可以在模块的go.mod文件中找到,通常是第一行。

例如:github.com/nikhilg-hub/todo/ToDoBackend

package_name -> 模块内部的包路径。

例如:orm

因此,导入语句将如下所示:

import "github.com/nikhilg-hub/todo/ToDoBackend/orm"

根据我的理解,我们需要指定模块名称+包名称,因为我们可能需要在两个或多个不同的模块中使用相同的包名称。

**注意:**即使从同一模块导入包,您仍然需要像上面那样指定完整的导入路径。

英文:

In simple words you can solve the import problem even with GO111MODULE=on with the following syntax for import:

import &lt;your_module_name&gt;/&lt;package_name&gt;

your_module_name -> module name which can be found in the go.mod file of the module as the first line.

example: github.com/nikhilg-hub/todo/ToDoBackend

package_name -> Path to your package within module.

example: orm

So the import statement would look like:

import &quot;github.com/nikhilg-hub/todo/ToDoBackend/orm&quot;

According to me we need to specify the module name + package name because we may need a same package name in two or more different modules.

Note: If you are importing a package from same module still you need to specify the full import path like above.

答案10

得分: 1

如果您有一个有效的$GOROOT$GOPATH,但是在这些目录之外进行开发,如果包(您自己的或其他人的)尚未下载,您可能会遇到此错误。

如果是这种情况,请尝试go get -d(-d标志防止安装),以确保在运行、构建或安装之前下载该包。

英文:

If you have a valid $GOROOT and $GOPATH but are developing outside of them, you might get this error if the package (yours or someone else's) hasn't been downloaded.

If that's the case, try go get -d (-d flag prevents installation) to ensure the package is downloaded before you run, build or install.

答案11

得分: 1

  • GOROOT 应该设置为你的安装目录 (/usr/local/go)。

  • GOPATH 应该设置为你的工作目录(类似于 /home/username/project_folder)。
    GOPATH 不应该设置为 GOROOT,因为你自己的项目可能需要安装包,而将这些包放在 Go 安装文件夹中是不推荐的。请查看此链接了解更多信息。

英文:
  • GOROOT should be set to your installation directory (/usr/local/go).

  • GOPATH should be set to your working directory (something like /home/username/project_folder).
    GOPATH should not be set to GOROOT as your own project may need to install packages, and it's not recommended to have those packages in the Go installation folder. Check out this link for more.

答案12

得分: 0

对我来说,以上的解决方案都没有起作用。但是我的 Go 版本不是最新的。我下载了最新版本,并在我的 Mac OS 上替换了旧版本,之后它就完美地运行了。

英文:

For me none of the above solutions worked. But my go version was not the latest one. I have downloaded the latest version and replaced the older version in my mac os after that it worked perfectly.

答案13

得分: 0

我在构建Docker文件时遇到了类似的问题:

[1/3] 步骤 9/9: 运行 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
api/v1alpha1/XXX.go:5:2: 在/workspace/client/YYY/YYY.go中找不到“.”包

只有在构建Dockerfile时才出现这个问题,本地构建正常。

问题最终是因为我的Dockerfile中缺少了一个语句:

COPY client/ client/
英文:

I had a similar problem when building a docker file:

[1/3] STEP 9/9: RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
api/v1alpha1/XXX.go:5:2: cannot find package &quot;.&quot; in:
        /workspace/client/YYY/YYY.go

This only appeared when building the Dockerfile, building locally worked fine.

The problem turned out to be a missing statement in my Dockerfile:

COPY client/ client/

答案14

得分: 0

package main

import (
"fmt"
"indexer/source/testPackage3" // 这将显示一个GOROOT错误。
"indexer/testPackage"
"indexer/testPackage2"
)

func main() {
fmt.Println("敏捷内容索引器 -")
fmt.Println(testPackage.Greeting())
fmt.Println(testPackage2.Greeting())
fmt.Println(testPackage3.Greeting())
}

英文:

I do not understand why this happens, we must be able to import from wherever our file is in its nest, since I have discovered that if we have more than one nest this will throw an error.

package main

import (
    &quot;fmt&quot; 
    &quot;indexer/source/testPackage3&quot; // this will be show a GOROOT error.
    &quot;indexer/testPackage&quot;
    &quot;indexer/testPackage2&quot;
)

func main() {
    fmt.Println(&quot;Agile content indexer -&quot;)
    fmt.Println(testPackage.Greeting())
    fmt.Println(testPackage2.Greeting())
    fmt.Println(testPackage3.Greeting())
}


├── testPackage2
│   ├── entry2.go
│   └── source
│       └── entry3.go

To conclude, I just want to tell you, the entry3.go file will not work when imported into my main file, which in this case is (main.go), I do not understand why, therefore, I have simply chosen to use a depth folder in the packages I need to export.

Go构建:"找不到包"(即使设置了GOPATH)

entry.go, entry2.go will work perfectly when imported, but entry3.go will not work..

In addition, both the directory and the name of the package must be the same so that they work properly when importing them.

答案15

得分: 0

在我的情况下,只有这样编译才成功...
首先,你需要查看你的GOROOT在哪里。使用以下命令查看它的位置:

$ go env

我将它链接到了我的包目录(我的包文件所在的目录,我在其中编写主包的代码)
使用以下命令创建符号链接:

$ ln -s <path_to_directory_with_package> <path_inside_your_GOROOT>

其次,你需要在主包的导入部分中添加所有新的包:

import (
	"fmt"
	"packag1"
	"os"
	"packag2"
)

在go代码文件中,包名的声明必须相同,像这样:

package packag2
package packag1

目录和go文件的名称必须相同:

$ tree .
.
├── main
├── main.go
├── packag1
│   └── packag1.go
└── packag2
    └── packag2.go

2 directories, 4 files

不要忘记创建符号链接:

$ tree /usr/local/go/src/
/usr/local/go/src
├── packag1 -> /home/user1name/Desktop/someDeskDir/src/packag1
└── packag2 -> /home/user1name/Desktop/someDeskDir/src/packag2

(我在这个注释中只显示了我创建的符号链接。在其他情况下,GOROOT目录中会有更多的文件和目录)

附注:
我使用的是“Ubuntu 22.04.2 LTS”,从https://go.dev/doc/install下载的go1.20.4.linux-amd64.tar.gz存档安装GO。
在其他系统上可能会有所不同。

英文:

In my case was successfully compiled only that way...
First. You mast look where is yours GOROOT. To see where it is, use command

$ go env

I put to it simliks linking to my packages directories (my directory's with package files located in some directory where I write a code of main package)
To create simlink use this

$ ln -s &lt;path_to_directory_with_package&gt; &lt;path_inside_your_GOROOT&gt;

Second. You mast add all new package in your import part of main package

import (
	&quot;fmt&quot;
	&quot;packag1&quot;
	&quot;os&quot;
	&quot;packag2&quot;
)

and in package name declaration, in go code files, mast be same names, like this

package packag2
package packag1

directories and go file names mast be named same names

$ tree .
.
├── main
├── main.go
├── packag1
│   └── packag1.go
└── packag2
    └── packag2.go

2 directories, 4 files

do not forget to make simlinks

$ tree /usr/local/go/src/
/usr/local/go/src
├── packag1 -&gt; /home/user1name/Desktop/someDeskDir/src/packag1
└── packag2 -&gt; /home/user1name/Desktop/someDeskDir/src/packag2

(I show in this comment only created simlinks by my. In other cases in GOROOT directory well be math more files and directory's)

P.S.
I use "Ubuntu 22.04.2 LTS", install GO from archive go1.20.4.linux-amd64.tar.gz, downloaded from https://go.dev/doc/install
In other systems mast be difference.

RU-RU-RU-RU

В моем случае компиляция заработала только при такой структуре...
Первое. Сначала надо узнать где Ваш GOROOT.Для этого есть команда

$ go env

Туда нужно поместить симлинки на директории где храняться Ваши пакеджи (я привык хранить код в одном месте в какой нибудь директории на рабочем столе, по этому и создаю симлинки в GOROOT. Наверное можно хранить исходники прям в GOROOT или еще как нибудь решить эту проблемму)

Создать симлинки можно этой командой

$ ln -s &lt;path_to_directory_with_package&gt; &lt;path_inside_your_GOROOT&gt;

Второе. Вы должны добавит все новые package в раздел import Вашего main package файла

import (
	&quot;fmt&quot;
	&quot;packag1&quot;
	&quot;os&quot;
	&quot;packag2&quot;
)

и в деклорации имени package, в файле с go кодом,должны быть те же имена

package packag2
package packag1

директории и файлы с кодом на языке go, должны называться точно так же

$ tree .
.
├── main
├── main.go
├── packag1
│   └── packag1.go
└── packag2
    └── packag2.go

2 directories, 4 files

не забудьте сделать симлинки

$ tree /usr/local/go/src/
/usr/local/go/src
├── packag1 -&gt; /home/user1name/Desktop/someDeskDir/src/packag1
└── packag2 -&gt; /home/user1name/Desktop/someDeskDir/src/packag2

(Я показал только те симлинки которые сам создал, обычнов GOROOT директории гораздо больше файлов)

З.Ы.
Я работаю на линукс "Ubuntu 22.04.2 LTS" устанавливал GO из скачаного с сайта https://go.dev/doc/install архива go1.20.4.linux-amd64.tar.gz
На других системах может быть по другому.

答案16

得分: -9

你尝试过将go的绝对目录添加到你的'path'中吗?

export PATH=$PATH:/directory/to/go/
英文:

Have you tried adding the absolute directory of go to your 'path'?

export PATH=$PATH:/directory/to/go/

huangapple
  • 本文由 发表于 2012年11月4日 06:15:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/13214029.html
匿名

发表评论

匿名网友

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

确定