duanjiati1755 2013-12-06 15:40
浏览 65
已采纳

在同一目录中有两个软件包是否有意义?

I have a project that provides a library (exports some funcs) and also must provide a command-line interface (there must be an executable file).

Example of directory structure:

whatever.io/
    myproject/
        main.go
        myproject.go

The go compiler needs the package main and func main to start execution. My library needs the package myproject where I put stuff on it. This is what the go tool says when I am building another project that tries to import myproject:

main.go:5:2: found packages myproject (myproject.go) and main (main.go) in $GOPATH/src/whatever.io/myproject

So I believe there is no way to do it.

Should I move the library or the CLI to another package?

  • 写回答

3条回答 默认 最新

  • dstwfcz1377 2013-12-06 16:14
    关注

    Just move your packages inside a new folder within the same directory of main.go. Remember to import the new package from the reference of the $GOPATH.

    Example:

    user@user:~/p/go/test/so-multipack$ ls -R
    .:
    a  main.go
    
    ./a:
    a.go
    user@user:~/p/go/test/so-multipack$ cat main.go 
    package main
    
    import (
        "../so-multipack/a"
    )
    func main(){
        a.Hello()
    }
    user@user:~/p/go/test/so-multipack$ cat a/a.go 
    package a
    import (
        "fmt"
    )
    func Hello(){
        fmt.Println("hello from a")
    }
    user@user:~/p/go/test/so-multipack$ go run main.go 
    hello from a
    user@user:~/p/go/test/so-multipack$ go build 
    user@user:~/p/go/test/so-multipack$ ls
    a  main.go  so-multipack
    user@user:~/p/go/test/so-multipack$ 
    

    Useful link:

    go build vs go build file.go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比