Go项目有2个可执行文件。

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

Go project with 2 executables

问题

大家好,我对Golang还比较新手,我正在编写一个玩具客户端和服务器应用程序,只是为了学习库。

但是我有一个项目文件夹:

philipherron@Philips-iMac {~/workspace/gospace/src/github.com/redbrain/station} $ echo $GOPATH
/Users/philipherron/workspace/gospace

我想要有两个可执行文件:

  • client.go
  • server.go

但是当我构建时,出现了以下错误:

philipherron@Philips-iMac {~/workspace/gospace/src/github.com/redbrain/station} $ go build github.com/redbrain/station/
# github.com/redbrain/station
./server.go:5: main redeclared in this block
    previous declaration at ./client.go:5

我猜这是因为看起来我在同一个包中创建了两个main函数。

所以我尝试创建一个client和一个server子目录,并在每个目录中放置可执行文件,但是我得到了以下错误:

philipherron@Philips-iMac {~/workspace/gospace/src/github.com/redbrain/station} $ go build github.com/redbrain/station/client
go install github.com/redbrain/station/client: build output "client" already exists and is a directory

我猜这是因为我的目录结构是这样的:

$ tree
.
├── client
│   └── client.go
└── server
    └── server.go

2 directories, 4 files

不确定如何解决这个问题,希望在同一个目录中同时拥有客户端和服务器,但也许这违背了我在Go中应该做的事情的规范?

英文:

Hi all I am fairly new to Golang, I am writing a toy client and server app just to learn the libraries.

But I have the project folder:

philipherron@Philips-iMac {~/workspace/gospace/src/github.com/redbrain/station} $ echo $GOPATH
/Users/philipherron/workspace/gospace

I wanted to have 2 binaries:

  • client.go
  • server.go

But when I build I get:

philipherron@Philips-iMac {~/workspace/gospace/src/github.com/redbrain/station} $ go build github.com/redbrain/station/
# github.com/redbrain/station
./server.go:5: main redeclared in this block
    previous declaration at ./client.go:5

I guess this is because it looks like I am making to mains in the same package.

So I tried creating a client and a server subdir and have the binaries in each of those, but I get:

philipherron@Philips-iMac {~/workspace/gospace/src/github.com/redbrain/station} $ go build github.com/redbrain/station/client
go install github.com/redbrain/station/client: build output "client" already exists and is a directory

I guess this is because I have the layout of:

$ tree
.
├── client
│   └── client.go
└── server
    └── server.go

2 directories, 4 files

Not sure how to get around this, it would just be nice to have the same client and server in the same directory but maybe this is against how I should be doing things in go?

答案1

得分: 8

只需将您的.go文件重命名即可。编译器试图写入'client',但是'client'已经被目录占用。

$ tree
.
├── client
│   └── main.go
└── server
    └── main.go

2 directories, 4 files

或者创建一个脚本,以不同的名称输出它们 go build -o client client/main.go

英文:

just rename your .go files. The compiler is trying to write to 'client' but 'client' is already taken by the directory.

$ tree
.
├── client
│   └── main.go
└── server
    └── main.go

2 directories, 4 files

And/Or create a script that outputs them with a different name go build -o client client/main.go

答案2

得分: 0

除了上面提到的单独的包之外,如果你设置GOBIN=$GOPATH/bin,它将在bin目录中创建客户端和服务器,并且不会与目录名称冲突。

英文:

along with with separate packages as above, if you set the GOBIN=$GOPATH/bin then it will create client and server in the bin dir and it will not collide with dir names

huangapple
  • 本文由 发表于 2014年6月19日 01:56:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/24292133.html
匿名

发表评论

匿名网友

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

确定