如何在 Go on Google Cloud Functions 中使用子包? [英] How can I use a sub-packages with Go on Google Cloud Functions?
问题描述
我想使用 Go Cloud Function 的帮助程序包.该包具有一些可以在多个函数之间共享的辅助逻辑.但是,构建包以便它们都能正常工作的正确方法是什么?包应该在同一个项目中 - 不作为一个完全独立的包发布和公开.
我在 Google 工作.此问题的目标是主动回答常见问题并帮助开发者从 Go on GCF 开始.
您可以通过 Go 使用子包模块.Go 模块是 Go 的新依赖项管理解决方案 - 它们让您可以在 GOPATH
之外工作,并让您管理您拥有的每个依赖项的确切版本.
模块还允许您定义一组具有相同导入路径前缀的 Go 包.在编写函数时,这可让您在模块中导入其他包.
您正在部署的函数需要位于模块的根目录.
这是一个示例文件结构以及如何导入包:
<预><代码>.├── cmd│ └── main.go # 用于测试.可以导入和设置您的功能.├── function.go # 可以导入example.com/foo/helperpackage├── function_test.go├── go.mod #模块example.com/foo└── 辅助包└── helper.go这个设置在 function.go
中有你的函数,并由 function_test.go
测试.它们位于名为 example.com/foo
的模块中.helperpackage
可以通过 function.go
使用 example.com/foo/helperpackage
导入.
这里还有一个cmd
目录,可能对本地测试有帮助.您可以导入 example.com/foo
并启动一个 HTTP 服务器,它将您的函数注册为 HTTP 处理程序.例如:
包主进口 (日志"网络/http"example.com/foo")功能主(){http.Handle("/HelloHTTP", foo.HelloHTTP)log.Fatal(http.ListenAndServe(":8080", nil))}
注意:您可以使用供应商目录来实现相同的结果.但是,您的函数导入的所有包都需要位于供应商目录中(具有完整的导入路径),这可行,但维护起来很麻烦.将子包复制到您的供应商目录中并不常见,因此我不建议这样做.
I'd like to use a helper package from Go Cloud Function. The package has some helper logic that can be shared between multiple functions. But, what is the right way to structure the packages so they all work? The package should be in the same project - not published and public as a completely separate package.
I work at Google. The goal for this question is to proactively answer common questions and help developers starting off with Go on GCF.
You can use subpackages with Go modules. Go modules are Go's new dependency management solution - they let you work outside of GOPATH
and let you manage the exact versions of each dependency you have.
Modules also let you define a group of Go packages with the same import path prefix. When you're writing a function, this lets you import other packages in your module.
The function you're deploying needs to be at the root of your module.
Here is an example file structure and how packages would be imported:
.
├── cmd
│ └── main.go # Useful for testing. Can import and setup your function.
├── function.go # Can import example.com/foo/helperpackage
├── function_test.go
├── go.mod # module example.com/foo
└── helperpackage
└── helper.go
This setup has your function in function.go
and tested by function_test.go
. They are in a module named example.com/foo
. helperpackage
can be imported by function.go
using example.com/foo/helperpackage
.
This also has a cmd
directory, which may be helpful for local testing. You can import example.com/foo
and start an HTTP server which registers your function as an HTTP handler. For example:
package main
import (
"log"
"net/http"
"example.com/foo"
)
func main() {
http.Handle("/HelloHTTP", foo.HelloHTTP)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Note: You could use a vendor directory to achieve the same result. But, all of the packages your function imports would need to be in the vendor directory (with the full import path), which works, but is cumbersome to maintain. It's uncommon to copy sub-packages into your vendor directory, so I wouldn't recommend this.
这篇关于如何在 Go on Google Cloud Functions 中使用子包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!