You need to enable JavaScript to run this app. Go使用yaml.v3反序列化报错cannot unmarshal !!seq into string如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Go使用yaml.v3反序列化报错cannot unmarshal !!seq into string如何解决

错误原因

ENV结构体中Attributes字段声明为string类型,但YAML配置里attributes对应的是字符串数组(YAML类型标识!!seq对应序列/数组类型),反序列化时类型不匹配触发报错。

修复方法

仅需要调整ENV结构体的字段类型,将Attributes的类型从string修改为[]string即可,修改后的结构体代码如下:

type ENV struct {
    Attributes []string `yaml:"attributes"`
}

如果需要打印三个数组元素,可以调整输出逻辑:

for k, v := range data {
    fmt.Printf("key: %v, attributes值: %v\n", k, v.Attributes)
}

内容的提问来源于stack exchange,提问作者Learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 22:48:00