Skip to content

Instantly share code, notes, and snippets.

@selfup
Last active December 31, 2018 19:29
Show Gist options
  • Save selfup/cb06aa5ecf80a4d60cc290658a82f42c to your computer and use it in GitHub Desktop.
Save selfup/cb06aa5ecf80a4d60cc290658a82f42c to your computer and use it in GitHub Desktop.
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