gpt4 book ai didi

go - 类型断言会改变go中的值吗?

转载 作者:IT王子 更新时间:2023-10-29 02:28:52 26 4
gpt4 key购买 nike

这里是新手。

我有一张 map ,其中关键参数应该是 []string

但是,如果我尝试直接使用该值 arguments := m["arguments"] 它似乎不是正确的类型。当稍后使用 arguments... 附加到另一个 slice 时,我得到 Cannot use 'arguments' (type interface{}) as type []string

我通过将赋值更改为类型检查 arguments, _ := m["arguments"].([]string) 来解决这个问题。那行得通,但我不确定为什么。是type assertion也进行转换?

完整示例如下:

import (
"github.com/fatih/structs"
"strings"
)

var playbookKeyDict = map[string]string{
"Playbook": "",
"Limit" : "--limit",
"ExtraVars" : "--extra-vars",
}

type Playbook struct {
Playbook string `json:"playbook" xml:"playbook" form:"playbook" query:"playbook"`
Limit string `json:"limit" xml:"limit" form:"limit" query:"limit"`
ExtraVars string `json:"extra-vars" xml:"extra-vars" form:"extra-vars" query:"extra-vars"`
Arguments []string `json:"arguments" xml:"arguments" form:"arguments" query:"arguments"`
Args []string
}

func (p *Playbook) formatArgs() {
// is it worth iterating through directly with reflection instead of using structs import?
// https://stackoverflow.com/questions/21246642/iterate-over-string-fields-in-struct
m := structs.Map(p)

// direct assignment has the wrong type?
// arguments := m["arguments"]
arguments, _ := m["arguments"].([]string)
delete(m, "arguments")

for k, v := range m {
// Ignore non-strings and empty strings
if val, ok := v.(string); ok && val != "" {
key := playbookKeyDict[k]
if key == "" {
p.Args = append(p.Args, val)
} else {
p.Args = append(p.Args, playbookKeyDict[k], val)
}
}
}
p.Args = append(p.Args, arguments...)
}

最佳答案

类型断言用于获取使用接口(interface)包装的值。

m := structs.Map(p)

Map(v interface{}){}

Map 函数实际上是以接口(interface)作为参数的。它包装了 []string 类型及其底层值 slice。可以使用 Relection 检查类型reflect.TypeOf()

func TypeOf(i interface{}) Type

根据 Russ Cox 博客 Interfaces

Interface values are represented as a two-word pair giving a pointer to information about the type stored in the interface and a pointer to the associated data.

Golang 中所述规范

For an expression x of interface type and a type T, the primary expression

x.(T)

asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.

对于错误部分:-

Cannot use 'arguments' (type interface{}) as type []string

我们首先需要使用类型断言从接口(interface)获取类型[]string 的基础值。

关于go - 类型断言会改变go中的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49394466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com