我正在研究一段使用 go 创建简单 lambda 函数的代码,该函数在调用时返回一个虚拟值。
package main
import (
"github.com/aws/aws-lambda-go/lambda"
)
type book struct {
ISBN string `json:"isbn"`
Title string `json:"title"`
Author string `json:"author"`
}
func show() (*book, error) {
bk := &book{
ISBN: "978-1420931693",
Title: "The Republic",
Author: "Plato",
}
return bk, nil
}
func main() {
lambda.Start(show)
}
在上面的代码中,我唯一不明白的是为什么我们要从show()
函数返回一个指针,以及这是如何解决的。如果我们返回实际book
变量而不是指针会发生什么。