英文:
cannot use req (variable of type events.APIGatewayProxyRequest) as core.SwitchableAPIGatewayRequest value in argument to adapter.ProxyWithContext
问题
我是你的中文翻译助手,以下是翻译好的内容:
我对Golang还不熟悉。我似乎无法提供适用于adapter.ProxyWithContext的参数。根据这个链接https://pkg.go.dev/github.com/awslabs/aws-lambda-go-api-proxy@v0.13.0/gorillamux#GorillaMuxAdapter.ProxyWithContext,它应该接受API Gateway代理事件或API Gateway V2事件。
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
routerProxy "github.com/awslabs/aws-lambda-go-api-proxy/gorillamux"
)
var adapter *routerProxy.GorillaMuxAdapter
func main() {
cfg := config.NewConfig()
s := app.NewApp(cfg, log)
adapter = routerProxy.New(s.SetupRoutes())
lambda.Start(lambdaHandler) // 执行lambda和特定的lead处理程序
}
func lambdaHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
c, err := adapter.ProxyWithContext(ctx, req)
return c, err
}
我遇到了这个错误:
``var req events.APIGatewayProxyRequest
cannot use req (variable of type events.APIGatewayProxyRequest) as core.SwitchableAPIGatewayRequest value in argument to adapter.ProxyWithContextcompilerIncompatibleAssign
英文:
I'm new to Golang. I can't seem to supply the argument needed to work with adapter.ProxyWithContext. According to this https://pkg.go.dev/github.com/awslabs/aws-lambda-go-api-proxy@v0.13.0/gorillamux#GorillaMuxAdapter.ProxyWithContext it should accept API Gateway proxy event or API Gateway V2 event.
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
routerProxy "github.com/awslabs/aws-lambda-go-api-proxy/gorillamux"
)
var adapter *routerProxy.GorillaMuxAdapter
func main() {
cfg := config.NewConfig()
s := app.NewApp(cfg, log)
adapter = routerProxy.New(s.SetupRoutes())
lambda.Start(lambdaHandler) // execute lambda and the specific lead handler
}
func lambdaHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
c, err := adapter.ProxyWithContext(ctx, req)
return c, err
}
i'm having this error:
``var req events.APIGatewayProxyRequest
cannot use req (variable of type events.APIGatewayProxyRequest) as core.SwitchableAPIGatewayRequest value in argument to adapter.ProxyWithContextcompilerIncompatibleAssign
</details>
# 答案1
**得分**: 1
如评论者[mkopriva](https://stackoverflow.com/users/965900/mkopriva)所指出的,你不能直接将`events.APIGatewayProxyRequest`传递给`adapter.ProxyWithContext`。
你需要创建一个新的`SwitchableAPIGatewayRequest`实例,并将其传递给`ProxyWithContext`。
这样应该可以解决你的问题:
```go
import "github.com/awslabs/aws-lambda-go-api-proxy/core"
[...]
func lambdaHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return adapter.ProxyWithContext(ctx, *core.NewSwitchableAPIGatewayRequestV1(&req))
}
英文:
As commenter mkopriva noted, you can not directly pass the events.APIGatewayProxyRequest
to adapter.ProxyWithContext
.
You need to create a new instance of SwitchableAPIGatewayRequest
and pass it to ProxyWithContext
.
This should fix your issue:
import "github.com/awslabs/aws-lambda-go-api-proxy/core"
[...]
func lambdaHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return adapter.ProxyWithContext(ctx, *core.NewSwitchableAPIGatewayRequestV1(&req))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论