为什么不能在golang中将主库添加到我的库中? [英] Why can't I add a main to my library in golang?

查看:56
本文介绍了为什么不能在golang中将主库添加到我的库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在完成应该是一件容易的事情上遇到了麻烦.我了解代码组织的GitHub模型(即库回购和使用该库的应用回购).我认为这太棒了.但是我经常发现我希望 mylib 与一个简单的可执行文件捆绑在一个 main.go 文件中. main.go 应该是 package main 包,并且应该导入 mylib .换句话说,它应该是有关如何构建使用此库的应用程序的确切文档.

I'm having trouble achieving what should be an easy task. I understand the GitHub model for code organization (ie a library repo and an app repo that consumes the library). I think it's fantastic. But I find often that I want mylib to come bundled with a simple executable in a single main.go file. The main.go should be package main and should import mylib. In other words, it should be an exact documentation of how to build an app that consumes this library.

我的观点是,由于提供包装您的库的简单命令行界面通常足够方便,因此应该有一种简单的方法来执行此操作,而不必进行其他回购,而golang应该会提供帮助.

My point is, since it is often enough convenient to provide a simple command line interface that wraps your library, there should be an easy way to do this without having to make another repo, and golang should help.

我想要以下内容:

$GOPATH/src/github.com/me/mylib
    mylib.go
    mylib_also.go
    main.go

其中 mylib 是库( package mylib ),而 main.go package main 并在运行时 go install 会生成 bin/mylib pkg/mylib.a .

where mylib is the library (package mylib) and main.go is package main and on running go install it generates bin/mylib and pkg/mylib.a.

main.go 应该导入"github.com/me/mylib" (如果现在执行此操作,则会出现周期性导入错误)或 go将了解发生了什么,因为应该内置此功能,并且回购中的一个 main.go 会生成exec.最好要求导入(并消除周期性误差)是更好的方法.

Either main.go should import "github.com/me/mylib" (if I do that now, I get cyclical import error) or go would understand what's happening since this feature should be built in and the one main.go in the repo generates the exec. Probably requiring the import (and dropping the cyclical error) is the better way.

现在,我必须做

$GOPATH/src/github.com/me/mylib
    mylib/
        mylib.go
    main.go

所以我必须导入github.com/me/mylib/mylib,这很荒谬.

So I have to import github.com/me/mylib/mylib which is ridiculous.

总而言之, go install 应该允许包的特殊情况,以及允许导入包并提供简单cli演示包API的main.GitHub模型促进了两个仓库,但是在一个仓库中进行简单的clis应该很容易!

In sum, go install should allow the special case of a package and a main which imports the package and provides a simple cli that demonstrates the packages API. The GitHub model promotes two repos, but it should be easy to do simple clis in one!

推荐答案

每个文件夹中不能有多个软件包.Go在程序包级别而非文件级别进行操作.

You can't have multiple packages per folder. Go operates on a package-level, not a file level.

在这种情况下,惯例(占用您的库的二进制文件)是使用包main创建一个 cmd 文件夹-即按照

Convention in this case—a binary that consumes your library—is to create a cmd folder with your package main - i.e. as per https://github.com/bradfitz/camlistore/tree/master/cmd or https://github.com/boltdb/bolt

在您的情况下,这将是:

In your case this would be:

$GOPATH/src/github.com/me/mylib
    mylib/
      mylib.go
      README.md
      cmd/
        your-lib-tool/
          main.go

这篇关于为什么不能在golang中将主库添加到我的库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆