如何解决Go项目中嵌套应用程序二进制文件中的依赖项?
This sounds stupid, but I am trying for build my new golang project for a while now and I am stuck with following error
can't load package: package github.com/kuskmen/yamq/cmd/yamq-client: found packages main (main.go) and yamqclient (yamq-client.go) in C:\projects\yamq\cmd\yamq-client
I know this should be straightforward to fix, but I come from .NET and I am still not experienced in Go projects and its dependency resolution model hence the struggle.
My project structure looks like so
/yamq
/cmd
/yamq-client // yamq client application binary
main.go // package main
yamq-client.go // package yamqclient
/yamq-server // yamq server application binary
main.go // package main
yamq-server.go // package yamqserver
go.mod // contains only "module github.com/kuskmen/yamq" for now
... // some library files that will probably be moved to /shared folder
so far so good, when I do go build
in outermost directory ( /yamq ) it is building successfully (or at least it is not showing any errors), but when I try to build either yamq-client
or yamq-server
binaries I get the aforementioned error and every time I try to google it or find something useful I got some old article or answer that dates back 2013-2016 that suggests something about $GOPATH
and etc which shouldn't be the case here since I am trying to use go modules.
Help a fellow .NET developer join Go community by explaining him how exactly modules work cause I found this and this useless or at least I am missing the point, thanks in advance!
这听起来很愚蠢,但我现在尝试构建我的新golang项目已有一段时间,因此我坚持以下 错误 p>
无法加载程序包:程序包github.com/kuskmen/yamq/cmd/yamq-client:找到了程序包main(main.go)和yamqclient(yamq -client.go)在C:\ projects \ yamq \ cmd \ yamq-client p> blockquote>
中,我知道这应该很容易解决,但我来自.NET 而且我仍然对Go项目及其依赖项解析模型还没有经验,因此很难。 p>
我的项目结构看起来像这样 p>
/ yamq / cmd / yamq-client // yamq客户端应用程序二进制 main.go //包main yamq-client.go //包yamqclient / yamq-server // yamq服务器应用程序二进制\ n main.go //包main yamq-server.go //包yamqserver go.mod //续 现在仅是“模块github.com/kuskmen/yamq” ... // //一些可能会移至/ shared文件夹的库文件 code> pre>
到目前为止,到目前为止,当我在最外面的目录(/ yamq)中执行
go code>时,它可以成功构建(或至少不显示任何错误),但是当我尝试构建任一
时 yamq-client code>或
yamq-server code>二进制文件我收到上述错误,每次尝试搜索它或发现有用的东西时,我都会得到一些旧文章或答案,该文章可追溯到2013-2016年, 建议有关
$ GOPATH code>之类的东西,在这里不应该这样,因为我正尝试使用go模块。 p>
帮助其他.NET开发人员加入Go 通过向他解释模块的工作原理,发现了 this 和 th 是没有用的,或者至少我没讲清楚,谢谢! p> div>
To follow up from my comment above:
From https://golang.org/doc/code.html:
- Go programmers typically keep all their Go code in a single workspace.
- A workspace contains many version control repositories (managed by Git, for example).
- Each repository contains one or more packages.
- Each package consists of one or more Go source files in a single directory.
- The path to a package's directory determines its import path.
For your project, I'd do something like this:
$ tree
.
├── clientlib
│ └── lib.go
├── cmd
│ ├── client
│ │ └── main.go
│ └── server
│ └── main.go
├── go.mod
└── serverlib
└── lib.go
5 directories, 5 files
$ cat go.mod
module myproject.com
The module name is arbitrary (could be github.com/yourname/yourproject
).
For the server side:
$ cat serverlib/lib.go
package serverlib
import "fmt"
func Hello() {
fmt.Println("Hello from serverlib.Hello")
}
$ cat cmd/server/main.go
package main
import (
"fmt"
"myproject.com/serverlib"
)
func main() {
fmt.Println("Running server")
serverlib.Hello()
}
And now we can build and run it:
$ go build -o server cmd/server/main.go
$ ./server
Running server
Hello from serverlib.Hello
The client side looks symmetrical.
Variations: you could name the .go
files in cmd/...
by their actual binary names - like server.go
and client.go
. The package in each is still main
, but then go build
creates an executable with the file's name (sans the .go
) without needing to -o
explicitly.