范围 []string 返回 uint8

huangapple go评论94阅读模式
英文:

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(&quot;Value ContainmentPath:&quot;, m.ContainmentPath)
	spew.Dump(&quot;Value v[1]:&quot;, v[1])		
	value = v[1]
}

When I run it I get this for ContainmentPath:

(string) (len=8) &quot;Value ContainmentPath:&quot;
([]string) (len=2 cap=2) {
 (string) (len=4) &quot;File&quot;,
 (string) (len=11) &quot;File::Value&quot;
}
(string) (len=8) &quot;Value ContainmentPath:&quot;
([]string) (len=3 cap=3) {
 (string) (len=7) &quot;Default&quot;,
 (string) (len=13) &quot;Default::Echo&quot;,
 (string) (len=13) &quot;Default[main]&quot;
}
(string) (len=8) &quot;Value ContainmentPath:&quot;
([]string) (len=3 cap=3) {
 (string) (len=5) &quot;Stage&quot;,
 (string) (len=10) &quot;Exec::File&quot;,
 (string) (len=10) &quot;Exec[File]&quot;
}

When I run it I get this for v[1]:

(string) (len=11) &quot;Value v[1]:&quot;
(uint8) 114
(string) (len=11) &quot;Value v[1]:&quot;
(uint8) 105
(string) (len=11) &quot;Value v[1]:&quot;
(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.

huangapple
  • 本文由 发表于 2021年8月26日 15:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/68934489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定