dongyou7472 2015-06-29 20:51
浏览 54
已采纳

在Go中构建CLI应用程序/程序包

I may be going about this incorrectly (I've read How to Write Go Code), but here's what I'm trying to achieve: I want to write a golang package that also come with a CLI, so that a user can use the package in their own programs but also use the CLI that I've built with it.

So what I have so far is this structure:

mypackage/
  -api/
    -account.go // package api
    -node.go    // package api
  -main.go // package main - this is where I want
           // the executable code to be that is put in the `bin` folder.

This is a very crude file structure as I'm just starting out the project, but that's what I'm trying to achieve rather than build a package with the usable code in it and then build a separate repo with my CLI in it. But when I attempt to run main.go, it says undefined: Account (which is a struct in account.go).

The contents of my code is unimportant right now, it's very basic as I'm just trying to get the code to work. Here's what I have.

account.go

package api

import (
    "io/ioutil"
    "net/http"
)

type Account struct {
    email         string
}

main.go

package main

import (
    "github.com/username/mypackage/api"
    "fmt"
)

func main() {
    a := Account{}

    fmt.Printf("%T", a)
}

If this is completely bad practice, I suppose I'll just make 2 repos, one with the CLI and another with my actual package code (again, usable in development by others by importing), but I'd really like it to be an all-in-one repo.

  • 写回答

1条回答 默认 最新

  • douxiaqin2062 2015-06-29 23:00
    关注

    There's no "one way", but the common approach is to make your top-level repo your library - i.e. github.com/you/somelib and then have a cmd directory underneath that contains your CLI (package mains) - there may even be many.

    e.g.

    yourlib/
        api.go
        api_test.go
        handler.go
        handler_test.go
        cmd/
            command-name/
                main.go // Builds a "command-name" binary
            other-command/
                main.go // Builds a "other-command" binary
    

    Your CLI apps are just consumers of your library. Users can retrieve it via go get github.com/you/yourlib/... which will install the lib and the binaries onto their GOPATH.

    The Go source itself, Camlistore and BoltDB all use this approach (amongst many other libs).

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀