Last active
December 31, 2018 19:29
-
-
Save selfup/cb06aa5ecf80a4d60cc290658a82f42c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
) | |
func main() { | |
first := []int{1, 2, 3} | |
second := []int{4, 5, 6} | |
zipped, err := ZipIntSlices(first, second) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Print(zipped) // [1 4 2 5 3 6] | |
} | |
func ZipIntSlices(firstSlice []int, secondSlice []int) ([]int, error) { | |
var zipSlice []int | |
firstSliceLength := len(firstSlice) | |
secondSliceLength := len(secondSlice) | |
if firstSliceLength != secondSliceLength { | |
return zipSlice, errors.New("Slices are not of equal length!") | |
} | |
for i := 0; i < firstSliceLength; i++ { | |
zipSlice = append(zipSlice, firstSlice[i]) | |
zipSlice = append(zipSlice, secondSlice[i]) | |
} | |
return zipSlice, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment