douchuang1861 2018-12-15 18:40
浏览 22
已采纳

混淆golang项目布局回购在实践中的工作方式

This question is in reference to the popular project-layout. Is this simply a way to lay out the code, but the actual compilation of a binary will be in the

/cmd/app1/
/cmd/app2/

So if I have a website, it would still be considered a cmd application that would simply start the http listener etc.

Or are there several "entry" points throughout the layout and not just in the cmd folders?

How exactly would you actual build and run your application using this layout? (or one of them since from what I understand it supports multiple)

is it like:

go build cmd/app1/*.go ?

  • 写回答

2条回答 默认 最新

  • doupa1883 2018-12-15 19:20
    关注

    You can just

    go build ./cmd/app/
    

    For example I have this module

    ├── cmd
    │   ├── cli
    │   │   └── main.go
    │   └── web
    │       └── main.go
    ├── go.mod
    └── service
        └── service.go
    

    go.mod is just

    module example
    

    service.go:

    package service
    
    import "fmt"
    
    func DoSomething() {
        fmt.Println("service processing")
    }
    

    cmd/web/main.go:

    package main
    
    import (
        "example/service"
        "fmt"
    )
    
    func main() {
        fmt.Println("starting web program")
        service.DoSomething()
    }
    

    cmd/cli/main.go:

    package main
    
    import (
        "example/service"
        "fmt"
    )
    
    func main() {
        fmt.Println("starting cli program")
        service.DoSomething()
    }
    

    Build (From root)

    Build web:

    go build ./cmd/web/
    

    This will create binary file web

    run web

    ./web
    // output:
    // starting web program
    // service processing
    

    Build cli:

    go build ./cmd/cli/
    

    run web

    ./web
    // output:
    // starting cli program
    // service processing
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题