I'm converting a program from python to golang, and I have this line that gets the first value within a nested list:x_values = map(operator.itemgetter(0), self.coords)
This command turns [[1,2],[2,3],[7,4]]
to [1,2,7]
.
Is there an equivalent of this in go?
The equivalent in Go would be a for
loop:
package main
import (
"fmt"
)
func main() {
a := make([][]int, 3)
a[0] = []int{1, 2}
a[1] = []int{2, 3}
a[2] = []int{7, 4}
b := make([]int, len(a))
for i, v := range a {
if len(v) > 0 {
b[i] = v[0]
}
}
fmt.Println(b)
}