【问题标题】:slice shift like function in go langgolang中的切片移位功能
【发布时间】:2017-03-11 00:46:46
【问题描述】:

数组移位函数如何与切片一起使用?

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}

    for k, v := range s {
        x, a := s[0], s[1:] // get and remove the 0 index element from slice
        fmt.Println(a) // print 0 index element
    }
}

我从切片技巧中找到了一个示例,但无法正确使用。

https://github.com/golang/go/wiki/SliceTricks

x, a := a[0], a[1:]

编辑你能解释一下为什么 x 在这里没有定义吗?

以答案为基础并与 SliceTricks 合并

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    fmt.Println(len(s), s)
    for len(s) > 0 {
    x, s = s[0], s[1:] // undefined: x
        fmt.Println(x) // undefined: x
    }
    fmt.Println(len(s), s)
}

【问题讨论】:

  • tmp/sandbox471444382/main.go:10: syntax error: unexpected { at end of statement
  • 是什么让你认为 slice 有 shift 方法?
  • github.com/golang/go/wiki/SliceTricks 提到了一个名为 Shift 的子标题
  • 这不是方法名。此外,Go 也没有 while 循环; for is Go's while.
  • 好的,抱歉我是这种语言的新手,在这种情况下的策略是什么,从一开始就删除一个元素,每次迭代并打印它

标签: go slice shift


【解决方案1】:

例如,

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    fmt.Println(len(s), s)
    for len(s) > 0 {
        x := s[0]      // get the 0 index element from slice
        s = s[1:]      // remove the 0 index element from slice
        fmt.Println(x) // print 0 index element
    }
    fmt.Println(len(s), s)
}

输出:

6 [2 3 5 7 11 13]
2
3
5
7
11
13
0 []

参考资料:

The Go Programming Language Specification: For statements


回答编辑问题的附录:

声明x

package main

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    fmt.Println(len(s), s)
    for len(s) > 0 {
        var x int
        x, s = s[0], s[1:]
        fmt.Println(x)
    }
    fmt.Println(len(s), s)
}

输出:

6 [2 3 5 7 11 13]
2
3
5
7
11
13
0 []

您可以为任何切片类型复制和粘贴我的代码;它推断x 的类型。如果s的类型发生变化,则不必更改。

for len(s) > 0 {
    x := s[0]      // get the 0 index element from slice
    s = s[1:]      // remove the 0 index element from slice
    fmt.Println(x) // print 0 index element
}

对于您的版本,x 的类型是显式的,如果 s 的类型发生更改,则必须进行更改。

for len(s) > 0 {
    var x int
    x, s = s[0], s[1:]
    fmt.Println(x)
}

【讨论】:

  • 感谢 Peter... 不知道 for 可以这样使用,非常类似于 while 循环
【解决方案2】:

简单解释一下我们如何实现类似 shift 的 Go 功能。这实际上是一个非常手动的过程。举个例子:

catSounds := []string{"meow", "purr", "schnurr"}

firstValue := stuff[0] // meow
catSounds = catSounds[1:]

在第一行,我们创建切片。

在第二行,我们得到了切片的第一个元素。

在第三行,我们将 catSounds 的值重新分配给当前位于 catSounds 中的第一个元素 (catSounds[1:]) 之后的所有内容。

因此,为了简洁,我们可以用逗号来压缩第二行和第三行:

catSounds := []string{"meow", "purr", "schnurr"}

firstValue, catSounds := catSounds[0], catSounds[1:]

【讨论】:

  • meowpurr 都指向 cat,因为 scnurr 指向 .....????如果我回到家对我的猫说schnurr,我真的不知道我会如何反应。因为,很突然,我会意识到一些以前称为猫的生物(可能不为科学、民间传说、存在维度等所知)一直依偎在我的腿上年。从道德的角度来看,您确实应该添加免责声明> CAUTION: READER DISCRETION ADVISED
  • schnurr 是德语的 purr。巧合(或许不是)schnurrbart 是德语中的小胡子。
  • ^ 我在 stackoverflow 上学到的东西:D
猜你喜欢
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 2022-10-21
  • 2014-11-01
  • 1970-01-01
相关资源
最近更新 更多