我正在导入以下
go
模块:
import "github.com/aws/aws-sdk-go-v2/service/ec2"
我想编写一个简单的函数来迭代实例标签,所以我正在执行以下操作
func getInstanceDescription(instance *ec2.Instance) string {
for _, tag := range instance.Tags {
if *tag.Key == "Name" || *tag.Key == "name" {
return *tag.Value
}
}
return *instance.InstanceId
}
然而,尽管Instance是这个包的一种类型,编译器还是抱怨
未定义:ec2.Instance编译器(UndeclaredImportedName)
我做错了什么?
您错误地使用了
Instance
结构。由于 Instance
结构体不在 ec2
包中,因此它位于 types
子包中。
你应该使用as
import "github.com/aws/aws-sdk-go-v2/service/ec2/types"
func getInstanceDescription(instance *types.Instance) string {
for _, tag := range instance.Tags {
if *tag.Key == "Name" || *tag.Key == "name" {
return *tag.Value
}
}
return *instance.InstanceId
}