Golang中如何使用Gitlab上的包
Golang中如何使用Gitlab上的包 大家好
我已从proto文件生成了一个go文件,并将其托管在 https://gitlab.com/silentdata/identity-contract。
然后我尝试创建一个gRPC服务器,如下所示:
package main
import (
"context"
"gitlab.com/silentdata/identity-contract/proto"
)
type server struct {
}
func (s *server) SignUp(ctx context.Context, in *Registration) (*Identity, error) {
}
func main() {
}
但是编译器报错:
./main.go:5:2: imported and not used: "gitlab.com/silentdata/identity-contract/proto"
./main.go:12:50: undefined: Registration
./main.go:12:66: undefined: Identity
我使用 go mod 来管理我的模块,文件夹结构如下:

我的 go.mod 文件内容
module identity-service
go 1.14
require (
github.com/golang/protobuf v1.3.4 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/labstack/echo/v4 v4.1.14 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
gitlab.com/silentdata/identity-contract v0.1.2
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
google.golang.org/grpc v1.27.1 // indirect
)
我哪里做错了?
谢谢
更多关于Golang中如何使用Gitlab上的包的实战教程也可以访问 https://www.itying.com/category-94-b0.html
我认为你可能需要使用 proto.Registration 和 proto.Identity?我已经有一段时间没有接触任何Go代码了 
更多关于Golang中如何使用Gitlab上的包的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
你好 @softshipper,正如 @flexd 提到的,你尝试过调用 proto.Identity 或 proto.Registration 吗?你构建时结果如何?
这里有一份关于导入和使用包的简短文档。我相信它将帮助你更好地理解包。
已经给出了正确的答案。另一个解决方案是使用 . 声明,从而无需包限定符即可访问导出的标识符。
import . "gitlab.com/silentdata/identity-contract/proto"
首先,错误信息指出你导入了 gitlab.com/silentdata/identity-contract/proto 但未使用它。这是因为你尝试在代码中使用 Registration 和 Identity,但编译器无法识别这些类型,因为它们可能不在你导入的包中,或者包路径不正确。
根据你的 go.mod 文件,你已添加依赖 gitlab.com/silentdata/identity-contract v0.1.2,但导入路径是 gitlab.com/silentdata/identity-contract/proto。这通常意味着 proto 是 GitLab 仓库中的一个子目录或子包。你需要确认 GitLab 仓库的结构,以确定正确的导入路径。
假设 GitLab 仓库 gitlab.com/silentdata/identity-contract 的结构如下(常见情况):
identity-contract/
├── proto/
│ └── *.pb.go # 生成的 Go 文件
├── go.mod
└── ...
那么,你的导入路径 gitlab.com/silentdata/identity-contract/proto 应该是正确的。但错误 undefined: Registration 和 undefined: Identity 表明,这些类型可能不在该包中,或者包名不匹配。
检查 GitLab 仓库中的 proto 包,确认生成的 Go 文件中是否定义了 Registration 和 Identity 类型。你可以通过查看仓库文件或使用 go doc 命令来验证。例如,运行以下命令查看包内容:
go doc gitlab.com/silentdata/identity-contract/proto
如果 Registration 和 Identity 确实在 proto 包中,那么问题可能是包名冲突或类型未导出。在 Go 中,只有首字母大写的类型才是导出的(即可从包外部访问)。确保 Registration 和 Identity 是导出的类型(即 type Registration struct{...} 而不是 type registration struct{...})。
另外,你的代码中 SignUp 方法的参数使用了 *Registration 和 *Identity,但未指定包前缀。如果这些类型在 proto 包中,你需要使用包名作为前缀,例如 *proto.Registration 和 *proto.Identity。
修改后的代码示例:
package main
import (
"context"
"gitlab.com/silentdata/identity-contract/proto"
)
type server struct {
// 嵌入 proto 包中的未实现服务,或直接实现方法
}
func (s *server) SignUp(ctx context.Context, in *proto.Registration) (*proto.Identity, error) {
// 实现方法逻辑
return &proto.Identity{}, nil
}
func main() {
// 主函数实现
}
如果 proto 包中定义了 gRPC 服务,你可能需要嵌入生成的 gRPC 服务结构体。例如,如果生成的代码中包含 type IdentityServiceServer interface,那么你的 server 结构体应实现该接口。示例:
type server struct {
proto.UnimplementedIdentityServiceServer // 假设生成的代码中有这个嵌入结构体
}
最后,确保你的模块依赖已正确下载。运行以下命令来同步依赖:
go mod tidy
go mod vendor # 可选,如果你使用 vendor 模式
如果问题仍然存在,检查 GitLab 仓库的 go.mod 文件,确认模块名称和版本是否匹配。有时,仓库可能使用不同的模块路径或版本标签。你可以尝试更新依赖到最新版本:
go get gitlab.com/silentdata/identity-contract/proto@latest
总结步骤:
- 确认导入路径和包结构。
- 使用正确的包前缀访问类型(如
proto.Registration)。 - 确保类型是导出的(首字母大写)。
- 同步和更新模块依赖。
如果以上步骤无法解决问题,请提供 GitLab 仓库中 proto 包的更多细节,例如生成的 Go 文件内容,以便进一步诊断。

