如何在Golang中生成一系列数字?

时间:2016-10-05 07:37:24

标签: arrays go initialization slice

我想在Golang中生成一系列数字,但我找不到任何内置函数。
基本上我想在Golang中使用PHP的range函数:

array range ( mixed $start , mixed $end [, number $step = 1 ] )

在创建数字类型的切片/数组时,您希望使用数字序列填充/初始化它。

3 个答案:

答案 0 :(得分:28)

Go标准库中没有等同于PHP的$body = Get-Content $computers | ForEach-Object { # Reset Variables $EventIds=$ServerName=$split=$null # Split the line in the text file on ; $split = $_ -split ";" # Get the servernamne and eventIds $ServerName = $split[0] [Array]$EventIds = $split[1] -split ',' # If there is an event id mentioned search for it else search for all events If($EventIds -ne "" -and $EventIds -ne $null){ Get-WinEvent -ComputerName $ServerName -FilterHashtable @{logname=$logname; Level=1,2,3; starttime=$StartDate; Id=$EventIds} -ErrorAction SilentlyContinue } Else{ Get-WinEvent -ComputerName $ServerName -FilterHashtable @{logname=$logname; Level=1,2,3; starttime=$StartDate} } } 。你必须自己创建一个。最简单的方法是使用range循环:

for

使用它:

func makeRange(min, max int) []int {
    a := make([]int, max-min+1)
    for i := range a {
        a[i] = min + i
    }
    return a
}

输出(在Go Playground上尝试):

a := makeRange(10, 20)
fmt.Println(a)

另请注意,如果范围很小,您可以使用composite literal

[10 11 12 13 14 15 16 17 18 19 20]

答案 1 :(得分: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]
}

答案 2 :(得分:0)

您可以使用makehttps://golang.org/pkg/builtin/#make)和for循环来生成任何类型的序列

seqSize = 11
seq := make([]int, seqSize)

for i := seq {
  seq[i] := i
}

fmt.Println(seq)

// results: [0, 1, 2, ... , 10]

for i := seq {
  seq[i] := 10 + i * 3 // for example 10 is start value and 3 is a step
}

fmt.Println(seq)

// results: [10, 13, 16, ... , 40]