2

I'm still trying to get my head around the Go workspace layout. I have a package, todinfo and a sample program which uses it, untod.

I currently have this arrangement of directories:

$GOPATH
+- bin...
+- pkg ...
+- src
   +- github.com
      +- longborough
         +- (others)...
         +- todinfo
            +- todinfo.go
            +- untod.go

I originally developed these two programs in separate directories. However, since untod is really part of the todinfo package, it seems more sensible to package it up as a single project, in the same directory.

But when I try to install (I've wrapped the reply onto three lines for clarity):

D:\Development\Go\src\github.com\longborough\todinfo>go install
 can''t load package: package github.com/longborough/todinfo: 
 found packages todinfo (todinfo.go) and main (untod.go) 
 in D:\Development\Go\src\github.com\longborough\todinfo

I hope I'm mistaken, but this smells a bit like Java, at least to the uninitiated.

What am I doing wrong? What Go commands should I use to install the package and then install the sample? Or, what is the correct directory arrangement?

Brent.Longborough
  • 9,567
  • 10
  • 42
  • 62
  • 5
    The go tools expect 1 package per directory. Everything in `todinfo` either need to be `package todinfo` or `package main`. – JimB Feb 01 '16 at 16:16
  • 2
    The document [How to Write Go Code](https://golang.org/doc/code.html) describes how to setup a workspace. – Charlie Tumahai Feb 01 '16 at 16:17
  • @JimB That's precisely what I've got. `todinfo.go` has `package todinfo` and `untod.go` has `package main`. – Brent.Longborough Feb 01 '16 at 17:58
  • @MuffinTop I had already read that document before coming here. It doesn't address my use case. – Brent.Longborough Feb 01 '16 at 17:59
  • 2
    @Brent.Longborough: no, they both need to be the *same* package, if they are in the same directory. You can't have both `package todinfo` and `package main` in the same directory. – JimB Feb 01 '16 at 18:01
  • The [section on libraries](https://golang.org/doc/code.html#Library) in code.html shows how arrange a workspace with a package and a program. – Charlie Tumahai Feb 01 '16 at 18:25
  • @Brent.Longborough -- note JimB's first comment says "1 package per directory," not "1 per file." It's common to put commands under a `cmd/[binary name]` dir; see [the top answer here](http://stackoverflow.com/questions/14867452/what-is-a-sensible-way-to-layout-a-go-project) for more. – twotwotwo Feb 01 '16 at 19:42
  • @Jimb Thanks for the clarification. I missed the *either*. – Brent.Longborough Feb 02 '16 at 18:16

1 Answers1

1

This got me too. Think of it this way: untod is not part of the todinfo package, it's a consumer of the todinfo package (library). In fact, main is not really a package at all, just a marker to say that it's got an entrypoint and should be compiled into a binary.

TLDR: you can put untod anywhere. In the root is probably sensible: it will then be named whatever the last dir component of your $GOPATH is. Alternatively, put it in cmd/untod/untod.go if you've got multiple binaries.

After some more development, you might consider making a separate repo like github.com/longborough/todinfo-bins to keep them apart.

Dave Cheney has some decent advice on the subject.

nfirvine
  • 1,479
  • 12
  • 23