在两个文件中使用相同的类型声明时,在Go中遇到错误。

huangapple go评论85阅读模式
英文:

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 (
	&quot;context&quot;
	&quot;net/http&quot;

	&quot;github.com/aws/aws-lambda-go/events&quot;
	&quot;github.com/aws/aws-lambda-go/lambda&quot;
	&quot;github.com/&lt;username&gt;/&lt;project&gt;/utils&quot;
)

type Response events.APIGatewayProxyResponse


func Handler(ctx context.Context) (Response, error) {


	return utils.StandardResponse(&quot;Successfully executed the create handler function&quot;, http.StatusOK), nil

}

func main() {
	lambda.Start(Handler)
}

And utils/responses.go is :

package utils

import (

	&quot;github.com/aws/aws-lambda-go/events&quot;
)

type Response events.APIGatewayProxyResponse

func StandardResponse(body string, statusCode int) Response {
	return events.APIGatewayProxyResponse{
		StatusCode: statusCode,
		Body:       string(body),
		Headers: map[string]string{
			&quot;Access-Control-Allow-Origin&quot;: &quot;*&quot;,
		},
	}
}

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(&quot;Successfully executed the create handler function&quot;, 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) {
...
}

huangapple
  • 本文由 发表于 2021年9月18日 19:06:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/69234075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定