英文:
Getting error in Go when using same Type declaration in two files
问题
我正在尝试在Go中编写一个lambda函数。
我的main.go
文件如下:
package main
import (
"context"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/<username>/<project>/utils"
)
type Response events.APIGatewayProxyResponse
func Handler(ctx context.Context) (Response, error) {
return utils.StandardResponse("成功执行创建处理程序函数", http.StatusOK), nil
}
func main() {
lambda.Start(Handler)
}
而utils/responses.go
文件如下:
package utils
import (
"github.com/aws/aws-lambda-go/events"
)
type Response events.APIGatewayProxyResponse
func StandardResponse(body string, statusCode int) Response {
return events.APIGatewayProxyResponse{
StatusCode: statusCode,
Body: string(body),
Headers: map[string]string{
"Access-Control-Allow-Origin": "*",
},
}
}
当我尝试构建项目时,我遇到以下错误:
# command-line-arguments
handlers/handler1/main.go:31:31: cannot use utils.StandardResponse("Successfully executed the create handler function", http.StatusOK) (type utils.Response) as type Response in return argument
然而,如果我停止使用type Response events.APIGatewayProxyResponse,并将Response
替换为events.APIGatewayProxyResponse
,则一切都能成功构建,没有错误。
我不确定为什么会发生这种情况。我不能在不同的文件中使用相同的类型吗?
英文:
I'm trying to write a lambda function in Go.
My main.go
:
package main
import (
"context"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/<username>/<project>/utils"
)
type Response events.APIGatewayProxyResponse
func Handler(ctx context.Context) (Response, error) {
return utils.StandardResponse("Successfully executed the create handler function", http.StatusOK), nil
}
func main() {
lambda.Start(Handler)
}
And utils/responses.go
is :
package utils
import (
"github.com/aws/aws-lambda-go/events"
)
type Response events.APIGatewayProxyResponse
func StandardResponse(body string, statusCode int) Response {
return events.APIGatewayProxyResponse{
StatusCode: statusCode,
Body: string(body),
Headers: map[string]string{
"Access-Control-Allow-Origin": "*",
},
}
}
When I try to build the project, I get the following error :
# command-line-arguments
handlers/handler1/main.go:31:31: cannot use utils.StandardResponse("Successfully executed the create handler function", http.StatusOK) (type utils.Response) as type Response in return argument
However, this error goes away if I stop using type Response events.APIGatewayProxyResponse and instead replace Response
with events.APIGatewayProxyResponse
, everything builds with no error.
I'm not sure why this is happening. Can I not use the same type in different files?
答案1
得分: 1
在你的main.go
代码中,不要再定义两次Response
(一次在main.go
中,一次在utils/responses.go
中),而是使用utils.Response
:
func Handler(ctx context.Context) (utils.Response, error) {
...
}
英文:
Instead of defining Response
twice (once in main.go
and once in utils/responses.go
), use utils.Response
in your main.go
code:
func Handler(ctx context.Context) (utils.Response, error) {
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论