dsvs50005 2018-10-28 19:32
浏览 166
已采纳

构建一个多可执行的Go项目

I am trying to build a micro-services architecture project in Go. I am currently using Go 1.11 which supports modules, so I put my root directory in an arbitrary chosen directory, outside of GOPATH.

If I am getting the micro-services architecture concepts right, although my microservices need to be independent, they can share dependencies (and I don't see another way to do stuff, is it?)

Below is my directory structure:

.
├── go.mod
├── lambda
│   └── account_create
│       └── main.go
├── readme.md
└── types
    ├── account.go
    ├── location.go
    ├── order.go
    ├── pricing.go
    ├── product.go
    └── types.go

Now the behavior I expected was to be able to run go build lambda/account_create and get an executable with that functionality so I can supply it to the respective AWS Lambda function.

However, when I run the command, I get:

can't load package: package lambda/account_create: unknown import path "lambda/account_create": cannot find module providing package lambda/account_create

Please explain me why this does not work and give me some advice on how a project like this should look.

Thank you very much!

Edit Here is my lambda/account_create/main.go file contents:

package account_create

import (
    "fmt"
    "roasted.ro/pkg/types"
)

func main() {
    account := types.UserAccount{Name: "Victor"}
    fmt.Printf("Welcome %s", account.Name)
}
  • 写回答

1条回答 默认 最新

  • 普通网友 2018-10-28 22:02
    关注

    To answer the latter part of your question (as the first half is more about preference), you need to do:

    go build ./lambda/account_create
    

    Notice the ./. go build takes in a package name or a path. If you don't prepend the path with a ./, it assumes you are giving it a package name.

    Edit: the package has to be main if you want an executable. According to the docs, you always have to use package main for projects that build an executable:

    Executable commands must always use package main.

    Edit: I'll add some opinions on layout:

    Consider which parts of your project are exported to other go projects. Meaning someone could import your package and expect to gain value and not have their code break when yours changes.

    That should be either in a github.com/you/pkg/somepath package or just github.com/you/somepath.

    Anything else (other than main packages) should be under a /internal package.

    I typically make a /cmd/myexecurable1 /cmd/myexecurable2 and so forth for each executable.

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)