dsvs50005 2018-10-28 19:32
浏览 166


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 (

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.

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



    • ¥40 基于51单片机实现球赛计分器功能
    • ¥15 关于#ubuntu#的问题:把PPT上的实验做一下,只需要实验截图就行(语言-r语言)
    • ¥15 cs2游戏画面卡住,应用程序sid与指挥者sid不匹配
    • ¥15 实验七:Pandas要有实验截图和代码
    • ¥15 TypeError: Make sure that the iterable only contains strings.
    • ¥35 电脑放图书馆,这是被黑了吗
    • ¥15 等高线中数据取消科学计数法
    • ¥15 Qt播放10路ffmpeg 视频流
    • ¥15 如何利用闲置机械硬盘变现
    • ¥15 信号处理中的凸优化问题