Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go project with 2 executables

Tags:

go

structure

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?

like image 383
redbrain Avatar asked Jun 18 '14 17:06

redbrain


1 Answers

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

like image 161
fabrizioM Avatar answered Oct 22 '22 01:10

fabrizioM