douguanci9158 2016-02-01 08:10
浏览 38
已采纳

Golang-如何通过示例程序来安排我的工作区?

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?

  • 写回答

1条回答 默认 最新

  • dongze8698 2016-02-01 15:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部