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

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

slice shift like function in go lang

问题

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

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

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

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

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

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

英文:

how array shift function works with slices?

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
	}
}

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

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

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

Edit can you please explain why x is undefined here?

Building upon the answer and merging with 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)
}

答案1

得分: 4

例如,

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]      // 从切片中获取索引为0的元素
        s = s[1:]      // 从切片中删除索引为0的元素
        fmt.Println(x) // 打印索引为0的元素
    }
    fmt.Println(len(s), s)
}

输出结果:

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

参考资料:

Go编程语言规范:For语句


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

声明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]      // 从切片中获取索引为0的元素
    s = s[1:]      // 从切片中删除索引为0的元素
    fmt.Println(x) // 打印索引为0的元素
}

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

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

For example,

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)
}

Output:

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

References:

The Go Programming Language Specification: For statements


Addendum to answer edit to question:

Declare 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)
}

Output:

6 [2 3 5 7 11 13]
2
3
5
7
11
13
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.

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
}

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

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

答案2

得分: 2

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

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

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

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

firstValue := catSounds[0] // meow

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

catSounds = catSounds[1:]

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

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

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:

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

firstValue := stuff[0] // meow
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:

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

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:

确定