如何生成数字序列
How to generate a sequence of numbers
我想在 Go 中生成一个数字序列,但找不到任何内置函数。
基本上我想要 Golang 中 PHP 的 range 函数的等价物:
array range ( mixed $start , mixed $end [, number $step = 1 ] )
在创建 slice/array 数字类型并且您想 populate/initialize 使用数字序列时它会很有用。
在 Go 标准库中没有与 PHP 的 range
等价的东西。你必须自己创建一个。最简单的是使用 for
循环:
func makeRange(min, max int) []int {
a := make([]int, max-min+1)
for i := range a {
a[i] = min + i
}
return a
}
使用它:
a := makeRange(10, 20)
fmt.Println(a)
输出(在 Go Playground 上尝试):
[10 11 12 13 14 15 16 17 18 19 20]
另请注意,如果范围较小,可以使用composite literal:
a := []int{1, 2, 3}
fmt.Println(a) // Output is [1 2 3]
1- 您可以使用:
//Create a slice containing a range of elements.
//
// start: First value of the sequence.
// end: The sequence is ended upon reaching the end value.
// step: step will be used as the increment between elements in the sequence.
// step should be given as a positive number.
//
//Return Values: Returns a slice of elements from start to end, inclusive.
func NewSlice(start, end, step int) []int {
if step <= 0 || end < start {
return []int{}
}
s := make([]int, 0, 1+(end-start)/step)
for start <= end {
s = append(s, start)
start += step
}
return s
}
在 The Go Playground 上试用:
package main
import "fmt"
//Create a slice containing a range of elements.
//
// start: First value of the sequence.
// end: The sequence is ended upon reaching the end value.
// step: step will be used as the increment between elements in the sequence.
// step should be given as a positive number.
//
//Return Values: Returns a slice of elements from start to end, inclusive.
func NewSlice(start, end, step int) []int {
if step <= 0 || end < start {
return []int{}
}
s := make([]int, 0, 1+(end-start)/step)
for start <= end {
s = append(s, start)
start += step
}
return s
}
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(s) // [0 1 2 3 4 5 6 7 8 9]
fmt.Println(NewSlice(10, 19, 1)) // [10 11 12 13 14 15 16 17 18 19]
fmt.Println(NewSlice(10, 28, 2)) // [10 12 14 16 18 20 22 24 26 28]
fmt.Println(NewSlice(-10, -1, 1)) // [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1]
}
2- 您可以使用:
// Returns a slice of elements with exact count.
// step will be used as the increment between elements in the sequence.
// step should be given as a positive, negative or zero number.
func NewSlice(start, count, step int) []int {
s := make([]int, count)
for i := range s {
s[i] = start
start += step
}
return s
}
在 The Go Playground 上试用:
package main
import "fmt"
func NewSlice(start, count, step int) []int {
s := make([]int, count)
for i := range s {
s[i] = start
start += step
}
return s
}
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(s) // [0 1 2 3 4 5 6 7 8 9]
fmt.Println(NewSlice(10, 10, 1)) // [10 11 12 13 14 15 16 17 18 19]
fmt.Println(NewSlice(10, 10, 2)) // [10 12 14 16 18 20 22 24 26 28]
fmt.Println(NewSlice(-1, 10, -1)) // [-1 -2 -3 -4 -5 -6 -7 -8 -9 -10]
fmt.Println(NewSlice(20, 10, 0)) // [20 20 20 20 20 20 20 20 20 20]
}
您可以尝试此代码 GenerateSequenceInt,它类似于 Python 的 range
:
package main
import (
"fmt"
"errors"
)
func GenerateSequenceInt(begin, end, step int) (sequence []int){
if step == 0 {
panic(errors.New("step must not be zero"))
}
count := 0
if (end > begin && step > 0) || (end < begin && step < 0) {
count = (end-step-begin)/step + 1
}
sequence = make([]int, count)
for i := 0; i < count; i, begin = i+1, begin+step {
sequence[i] = begin
}
return
}
func main() {
seq1 := GenerateSequenceInt(-1,11,-3)
fmt.Println(seq1)
seq2 := GenerateSequenceInt(1,-1,3)
fmt.Println(seq2)
seq3 := GenerateSequenceInt(1,1,1)
fmt.Println(seq3)
seq4 := GenerateSequenceInt(1, 11, 2)
fmt.Println(seq4)
seq5 := GenerateSequenceInt(1, -11, -2)
fmt.Println(seq5)
}
package main
import "fmt"
func main() {
var a [11]int
for i := 1; i < len(a); i++ {
a[i] = i
}
fmt.Print(a)
}
您将获得:
[0 1 2 3 4 5 6 7 8 9 10]
我想在 Go 中生成一个数字序列,但找不到任何内置函数。
基本上我想要 Golang 中 PHP 的 range 函数的等价物:
array range ( mixed $start , mixed $end [, number $step = 1 ] )
在创建 slice/array 数字类型并且您想 populate/initialize 使用数字序列时它会很有用。
在 Go 标准库中没有与 PHP 的 range
等价的东西。你必须自己创建一个。最简单的是使用 for
循环:
func makeRange(min, max int) []int {
a := make([]int, max-min+1)
for i := range a {
a[i] = min + i
}
return a
}
使用它:
a := makeRange(10, 20)
fmt.Println(a)
输出(在 Go Playground 上尝试):
[10 11 12 13 14 15 16 17 18 19 20]
另请注意,如果范围较小,可以使用composite literal:
a := []int{1, 2, 3}
fmt.Println(a) // Output is [1 2 3]
1- 您可以使用:
//Create a slice containing a range of elements.
//
// start: First value of the sequence.
// end: The sequence is ended upon reaching the end value.
// step: step will be used as the increment between elements in the sequence.
// step should be given as a positive number.
//
//Return Values: Returns a slice of elements from start to end, inclusive.
func NewSlice(start, end, step int) []int {
if step <= 0 || end < start {
return []int{}
}
s := make([]int, 0, 1+(end-start)/step)
for start <= end {
s = append(s, start)
start += step
}
return s
}
在 The Go Playground 上试用:
package main
import "fmt"
//Create a slice containing a range of elements.
//
// start: First value of the sequence.
// end: The sequence is ended upon reaching the end value.
// step: step will be used as the increment between elements in the sequence.
// step should be given as a positive number.
//
//Return Values: Returns a slice of elements from start to end, inclusive.
func NewSlice(start, end, step int) []int {
if step <= 0 || end < start {
return []int{}
}
s := make([]int, 0, 1+(end-start)/step)
for start <= end {
s = append(s, start)
start += step
}
return s
}
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(s) // [0 1 2 3 4 5 6 7 8 9]
fmt.Println(NewSlice(10, 19, 1)) // [10 11 12 13 14 15 16 17 18 19]
fmt.Println(NewSlice(10, 28, 2)) // [10 12 14 16 18 20 22 24 26 28]
fmt.Println(NewSlice(-10, -1, 1)) // [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1]
}
2- 您可以使用:
// Returns a slice of elements with exact count.
// step will be used as the increment between elements in the sequence.
// step should be given as a positive, negative or zero number.
func NewSlice(start, count, step int) []int {
s := make([]int, count)
for i := range s {
s[i] = start
start += step
}
return s
}
在 The Go Playground 上试用:
package main
import "fmt"
func NewSlice(start, count, step int) []int {
s := make([]int, count)
for i := range s {
s[i] = start
start += step
}
return s
}
func main() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(s) // [0 1 2 3 4 5 6 7 8 9]
fmt.Println(NewSlice(10, 10, 1)) // [10 11 12 13 14 15 16 17 18 19]
fmt.Println(NewSlice(10, 10, 2)) // [10 12 14 16 18 20 22 24 26 28]
fmt.Println(NewSlice(-1, 10, -1)) // [-1 -2 -3 -4 -5 -6 -7 -8 -9 -10]
fmt.Println(NewSlice(20, 10, 0)) // [20 20 20 20 20 20 20 20 20 20]
}
您可以尝试此代码 GenerateSequenceInt,它类似于 Python 的 range
:
package main
import (
"fmt"
"errors"
)
func GenerateSequenceInt(begin, end, step int) (sequence []int){
if step == 0 {
panic(errors.New("step must not be zero"))
}
count := 0
if (end > begin && step > 0) || (end < begin && step < 0) {
count = (end-step-begin)/step + 1
}
sequence = make([]int, count)
for i := 0; i < count; i, begin = i+1, begin+step {
sequence[i] = begin
}
return
}
func main() {
seq1 := GenerateSequenceInt(-1,11,-3)
fmt.Println(seq1)
seq2 := GenerateSequenceInt(1,-1,3)
fmt.Println(seq2)
seq3 := GenerateSequenceInt(1,1,1)
fmt.Println(seq3)
seq4 := GenerateSequenceInt(1, 11, 2)
fmt.Println(seq4)
seq5 := GenerateSequenceInt(1, -11, -2)
fmt.Println(seq5)
}
package main
import "fmt"
func main() {
var a [11]int
for i := 1; i < len(a); i++ {
a[i] = i
}
fmt.Print(a)
}
您将获得:
[0 1 2 3 4 5 6 7 8 9 10]