英文:
Range []string returns uint8
问题
我有一个当前写成以下形式的yaml文件:
resource:
File:
containment_path:
- File
- File::Value
Default:
containment_path:
- Default
- Default::Echo
- Default[main]
Exec:
containment_path:
- Stage
- Exec::File
- Exec[File]
我使用结构体来获取containment_path:
type Yaml struct {
ContainmentPath []string `yaml:"containment_path"`
}
我想遍历ContainmentPath来获取containment_path中的第二个值:File::Value,Default::Echo,Exec::File。
我写了以下代码:
for _, v := range m.ContainmentPath {
spew.Dump("Value ContainmentPath:", m.ContainmentPath)
spew.Dump("Value v[1]:", v[1])
value = v[1]
}
当我运行它时,ContainmentPath的输出如下:
(string) (len=8) "Value ContainmentPath:"
([]string) (len=2 cap=2) {
(string) (len=4) "File",
(string) (len=11) "File::Value"
}
(string) (len=8) "Value ContainmentPath:"
([]string) (len=3 cap=3) {
(string) (len=7) "Default",
(string) (len=13) "Default::Echo",
(string) (len=13) "Default[main]"
}
(string) (len=8) "Value ContainmentPath:"
([]string) (len=3 cap=3) {
(string) (len=5) "Stage",
(string) (len=10) "Exec::File",
(string) (len=10) "Exec[File]"
}
当我运行它时,v[1]的输出如下:
(string) (len=11) "Value v[1]:"
(uint8) 114
(string) (len=11) "Value v[1]:"
(uint8) 105
(string) (len=11) "Value v[1]:"
(uint8) 116
我想要获取v[1]的值:File::Value,Default::Echo,Exec::File。如何在Go中实现这个?谢谢。
英文:
I have a yaml file that is currently written as:
resource:
File:
containment_path:
- File
- File::Value
Default:
containment_path:
- Default
- Default::Echo
- Default[main]
Exec:
containment_path:
- Stage
- Exec::File
- Exec[File]
I use structure to get containment_path:
type Yaml struct {
ContainmentPath []string `yaml:"containment_path"`
}
And i want to range over ContainmentPath to get the second value in containment_path : File::Value, Default::Echo, Exec::File <br>
I do this code : <br>
for _, v := range m.ContainmentPath {
spew.Dump("Value ContainmentPath:", m.ContainmentPath)
spew.Dump("Value v[1]:", v[1])
value = v[1]
}
When I run it I get this for ContainmentPath:
(string) (len=8) "Value ContainmentPath:"
([]string) (len=2 cap=2) {
(string) (len=4) "File",
(string) (len=11) "File::Value"
}
(string) (len=8) "Value ContainmentPath:"
([]string) (len=3 cap=3) {
(string) (len=7) "Default",
(string) (len=13) "Default::Echo",
(string) (len=13) "Default[main]"
}
(string) (len=8) "Value ContainmentPath:"
([]string) (len=3 cap=3) {
(string) (len=5) "Stage",
(string) (len=10) "Exec::File",
(string) (len=10) "Exec[File]"
}
When I run it I get this for v[1]:
(string) (len=11) "Value v[1]:"
(uint8) 114
(string) (len=11) "Value v[1]:"
(uint8) 105
(string) (len=11) "Value v[1]:"
(uint8) 116
And I want to get for v[1] the values : File::Value, Default::Echo, Exec::File <br>
How I can do this in Go ? <br>
Thanks
答案1
得分: 3
你正在对一个string
进行索引,而不是[]string
。
ContainmentPath
是一个字符串切片。
当你运行for _, v := range m.ContainmentPath
时,v
会取得切片中每个字符串的值。所以v
的类型是string
。
当你打印v[1]
时,你正在对字符串进行索引(没有先检查它的长度,这是一个非常糟糕的主意),并打印字符串中的第二个字符,它是一个字节。字节只是uint8
的别名。
英文:
You are indexing into a string
, not a []string
.
ContainmentPath
is a slice of strings.
When you run for _, v := range m.ContainmentPath
, v
is taking on the value of each string in the slice. So v
is of type string
.
When you print v[1]
, you are indexing into the string (without checking its length first, very bad idea) and printing the second character in the string which is a byte. A byte is just an alias for uint8.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论