无法使用aws-sdk-v2 golang模块中的实例类型

问题描述 投票:0回答:1

我正在导入以下

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)

我做错了什么?

amazon-web-services go aws-sdk aws-sdk-go
1个回答
0
投票

您错误地使用了

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
}

© www.soinside.com 2019 - 2024. All rights reserved.