dongwei2610 2016-10-05 07:37
浏览 876
已采纳

如何在Golang中生成数字序列?

I want to generate a sequence of numbers in Golang but I can't find any built-in functions for this.
Basically I want the equivalent of PHP's range function in Golang:

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

It would be useful when creating a slice/array of numeric types and you want to populate/initialize it with a numeric sequence.

  • 写回答

2条回答 默认 最新

  • dongshuan8722 2016-10-05 07:50
    关注

    There is no equivalent to PHP's range in the Go standard library. You have to create one yourself. The simplest is to use a for loop:

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

    Using it:

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

    Output (try it on the Go Playground):

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

    Also note that if the range is small, you can use a composite literal:

    a := []int{1, 2, 3}
    fmt.Println(a) // Output is [1 2 3]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dst67283 2016-10-05 08:03
    关注

    1- You may use:

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

    Try it on 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- You may use:

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

    Try it on 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]
    }
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 单片机实验pc软件完全,可以私我商议
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 一道python的homework题,老是非零返回求解
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
  • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有
  • ¥20 h3c静态路要求有详细过程
  • ¥15 调制识别中输入为时频图,星座图,眼图等
  • ¥15 数据结构C++的循环、随机数问题