To convert an array of strings to an array of integers in Go, you can use a loop to parse each string element into an integer.
In this example,
strArray
containing string representations of integers.intArray
with the same length as strArray
.strArray
into an integer and store it in intArray
.intArray
to demonstrate the conversion.package main
import (
"fmt"
"strconv"
)
func main() {
strArray := []string{"10", "20", "30"}
intArray := make([]int, len(strArray))
for i, str := range strArray {
intVal, err := strconv.Atoi(str)
if err == nil {
intArray[i] = intVal
}
}
fmt.Println("Converted array of integers:", intArray)
}
Converted array of integers: [10 20 30]
In this tutorial, we learned How to Convert an Array of Strings to an Array of Integers in Go language with well detailed examples.