在Go语言中,可以使用切片(slice)和移位(shift)操作来实现类似的功能。

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

slice shift like function in go lang

问题

数组的位移函数(array shift function)是指将数组中的元素向左或向右移动指定的位置。在Go语言中,可以使用切片(slice)来实现数组的位移操作。

在你提供的代码示例中,使用了切片来实现数组的位移。具体来说,通过以下代码实现了从切片中获取并移除第一个元素的操作:

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

这行代码中,s[0]表示获取切片s的第一个元素,s[1:]表示获取从第二个元素到最后一个元素的切片。通过将第一个元素赋值给变量x,并将剩余的切片赋值给变量a,实现了获取并移除第一个元素的操作。

关于你提到的x在代码中被标记为未定义的问题,可能是因为你在循环中多次使用了同名的变量x。在每次循环迭代时,变量x都会被重新声明和赋值,因此在循环外部的代码块中,变量x是未定义的。

希望这能解答你的问题!如果还有其他疑问,请随时提问。

英文:

how array shift function works with slices?

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := []int{2, 3, 5, 7, 11, 13}
  5. for k, v := range s {
  6. x, a := s[0], s[1:] // get and remove the 0 index element from slice
  7. fmt.Println(a) // print 0 index element
  8. }
  9. }

I found an example from slice tricks but can't get it right.

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

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

Edit can you please explain why x is undefined here?

Building upon the answer and merging with SliceTricks

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

答案1

得分: 4

例如,

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := []int{2, 3, 5, 7, 11, 13}
  5. fmt.Println(len(s), s)
  6. for len(s) > 0 {
  7. x := s[0] // 从切片中获取索引为0的元素
  8. s = s[1:] // 从切片中删除索引为0的元素
  9. fmt.Println(x) // 打印索引为0的元素
  10. }
  11. fmt.Println(len(s), s)
  12. }

输出结果:

  1. 6 [2 3 5 7 11 13]
  2. 2
  3. 3
  4. 5
  5. 7
  6. 11
  7. 13
  8. 0 []

参考资料:

Go编程语言规范:For语句


附加回答编辑到问题的补充:

声明x

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := []int{2, 3, 5, 7, 11, 13}
  5. fmt.Println(len(s), s)
  6. for len(s) > 0 {
  7. var x int
  8. x, s = s[0], s[1:]
  9. fmt.Println(x)
  10. }
  11. fmt.Println(len(s), s)
  12. }

输出结果:

  1. 6 [2 3 5 7 11 13]
  2. 2
  3. 3
  4. 5
  5. 7
  6. 11
  7. 13
  8. 0 []

你可以复制粘贴我的代码到任何切片类型中;它会推断出x的类型。如果s的类型发生变化,它不需要改变。

  1. for len(s) > 0 {
  2. x := s[0] // 从切片中获取索引为0的元素
  3. s = s[1:] // 从切片中删除索引为0的元素
  4. fmt.Println(x) // 打印索引为0的元素
  5. }

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

  1. for len(s) > 0 {
  2. var x int
  3. x, s = s[0], s[1:]
  4. fmt.Println(x)
  5. }
英文:

For example,

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

Output:

  1. 6 [2 3 5 7 11 13]
  2. 2
  3. 3
  4. 5
  5. 7
  6. 11
  7. 13
  8. 0 []

References:

The Go Programming Language Specification: For statements


Addendum to answer edit to question:

Declare x,

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := []int{2, 3, 5, 7, 11, 13}
  5. fmt.Println(len(s), s)
  6. for len(s) > 0 {
  7. var x int
  8. x, s = s[0], s[1:]
  9. fmt.Println(x)
  10. }
  11. fmt.Println(len(s), s)
  12. }

Output:

  1. 6 [2 3 5 7 11 13]
  2. 2
  3. 3
  4. 5
  5. 7
  6. 11
  7. 13
  8. 0 []

You can copy and paste my code for any slice type; it infers the type for x. It doesn't have to be changed if the type of s changes.

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

For your version, the type for x is explicit and must be changed if the type of s is changed.

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

答案2

得分: 2

在Go语言中,实现类似_shift_功能的方法是一个相当手动的过程。以下是一个简单的解释:

首先,我们创建一个切片:

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

然后,我们获取切片的第一个元素:

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

接下来,我们将catSounds的值重新赋给切片中除第一个元素外的所有元素(catSounds[1:]):

  1. catSounds = catSounds[1:]

为了简洁起见,我们可以将第二行和第三行合并为一行,使用逗号分隔:

  1. catSounds := []string{"meow", "purr", "schnurr"}
  2. firstValue, catSounds := catSounds[0], catSounds[1:]
英文:

Just a quick explanation on how we implement shift-like functionality Go. It's actually a very manual process. Take this example:

  1. catSounds := []string{"meow", "purr", "schnurr"}
  2. firstValue := stuff[0] // meow
  3. catSounds = catSounds[1:]

On the first line, we create our slice.

On the second line we get the first element of the slice.

On the third line, we re-assign the value of catSounds to everything currently in catSounds after the first element (catSounds[1:]).

So given all that, we can condense the second and third lines with a comma for brevity:

  1. catSounds := []string{"meow", "purr", "schnurr"}
  2. firstValue, catSounds := catSounds[0], catSounds[1:]

huangapple
  • 本文由 发表于 2017年3月11日 08:46:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/42730031.html
匿名

发表评论

匿名网友

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

确定