2016-10-05 5 views
7

Ich möchte eine Sequenz von Zahlen in Golang erzeugen, aber ich kann keine eingebauten Funktionen dafür finden.
Grundsätzlich möchte ich das Äquivalent von range Funktion PHP in Golang:Wie erstelle ich eine Sequenz von Zahlen in Golang?

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

Es wäre sinnvoll, wenn eine Scheibe/Array von numerischen Typen zu schaffen und Sie wollen es mit einer numerischen Sequenz aufzufüllen/initialisieren.

Antwort

12

Es gibt keine Entsprechung zu PHP range in der Go-Standardbibliothek. Sie müssen selbst eines erstellen. Die einfachste ist es, eine for Schleife zu verwenden:

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

es verwenden:

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

Output (versuchen Sie es auf dem Go Playground):

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

Beachten Sie auch, dass, wenn der Bereich klein ist Sie können einen composite literal:

a := []int{1, 2, 3} 
fmt.Println(a) // Output is [1 2 3] 
verwenden
3

1- können Sie verwenden:

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

Ausprobieren 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- Sie verwenden können:

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

Ausprobieren 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] 
} 
Verwandte Themen