對于如何使用 golang 并坐在HttpApi后面將自定義型別傳遞到我的 Lambda 函式中,我有點困惑。
考慮以下 go lambda 處理程式,它幾乎是檔案中示例的副本。
type MyRequestType struct {
Name string `json:"name"`
Age int `json:"age"`
}
type MyResponseType struct {
Message string `json:"message"`
}
func handler(request MyRequestType) (MyResponseType, error) {
log.Printf("received request: %v", request)
return MyResponseType{Message: fmt.Sprintf("Hello %s, you are %d years old!", request.Name, request.Age)}, nil
}
func main() {
lambda.Start(handler)
}
結果訊息始終如下。
{
"message": "Hello , you are 0 years old!"
}
我有一種轉儲的感覺,這是不可能的Amazon API Gateway HTTP API。但我也沒有找到任何檔案指出這是不可能的。所以我真的想知道,如果我做錯了什么?
該檔案還說明了有關有效簽名的一些資訊:
例如func (context.Context, TIn) (TOut, error)
如果我使用的HTTP API是Payload format version 2:
是context.Context普通的golangcontext還是特別的?我在想events.APIGatewayV2HTTPRequestContext或其他人。
TInand TOut=> events.APIGatewayV2HTTPRequestand的正確型別是events.APIGatewayV2HTTPResponse什么?
uj5u.com熱心網友回復:
context.Context 是不是普通的golang背景關系
是的。
但是您可以獲得lambdacontext.FromContext包含額外 lambda 特定元資料的 Lambda 背景關系。
什么是正確的 TIn 和 TOut 型別
這取決于誰呼叫了 Lambda。當 Lambda 被另一個 AWS 服務呼叫時,包括 API 網關,所謂的TIn和TOut是來自lambdaevent包的型別。參考包介紹:
此程式包為處理 AWS 事件的 Lambda 函式提供輸入型別。
在 API 網關的情況下,這將是events.APIGatewayProxyRequestand Response,或者可能是在 v1.16.0 中添加的Payload format version 2and events.APIGatewayV2HTTPRequest。Response
github repo README中的更多檔案(但不多)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/417949.html
標籤:
