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条)

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面