I'm a beginner with Go. Converting a slice of ints to a slice of strings seems simple with a few lines:
nums := []int{1, 2, 3, 4}
sNums := make([]string, len(nums))
for i, x := range nums {
sNums[i] = strconv.Itoa(x)
}
However, I am wondering if there is a way to do it using a quick one-liner using built-ins/standard library, similar to the following in Python:
sNums = map(str, nums)
What you want can be done with a single line, but those solutions are inferior (in performance) to the simple loop you presented in the question. So here come some one-liners, but I would probably never use them to achieve what you want, so just use a for
loop, and if you have to do this many times, create a helper function (that will use the loop), and just call the helper function.
The following one-liners all build on the fmt.Sprint()
function, which generates a string representation for int
slices like this (without the quotes):
"[e1 e2 e3...]"
Where e1
, e2
... are the elements of the input slice.
So for example:
nums := []int{1, 2, -3, 4}
s := fmt.Sprint(nums)
fmt.Printf("%q", s)
Will output (try it on the Go Playground):
"[1 2 -3 4]"
We may "transform" this single string
value to its elements using 2 steps:
And we're done.
For the first step, we have several options, e.g. strings.Replace()
, strings.Replacer
, strings.Trim()
. We may "spice" up the trimming by slicing the string, to cut its first character (e.g. s[1:]
) so then it will be only needed to remove the trailing ']'
.
For the second step, we have strings.Split()
.
Without all the possible combinations, here are 3 one-liner examples to convert an int
slice to a string
slice:
nums := []int{1, 2, -3, 4}
sNums := strings.Split(strings.Replace(fmt.Sprint(nums)[1:], "]", "", -1), " ")
fmt.Printf("%q\n", sNums)
sNums2 := strings.Split(strings.Trim(fmt.Sprint(nums), "[]"), " ")
fmt.Printf("%q\n", sNums2)
sNums3 := strings.Split(strings.TrimRight(fmt.Sprint(nums)[1:], "]"), " ")
fmt.Printf("%q\n", sNums3)
Output of the above (try it on the Go Playground):
["1" "2" "-3" "4"]
["1" "2" "-3" "4"]
["1" "2" "-3" "4"]
There is one edge case that none of the above solutions handle: if the input is an empty slice, the output will be a slice containing an empty string (when it should be an empty slice). The reason for this is because if the input does not contain the space separator (in this case it's the empty string), strings.Split()
returns the input (which will be the empty string).
To handle the empty-input-slice edge case, we may use the strings.SplitN()
function instead of strings.Split()
, passing len(nums)
as the max number of parts we want. So if the input slice is empty, the output slice will also be empty:
nums := []int{}
sNums := strings.SplitN(strings.Replace(fmt.Sprint(nums)[1:], "]", "", -1), " ", len(nums))
fmt.Printf("%q\n", sNums)
sNums2 := strings.SplitN(strings.Trim(fmt.Sprint(nums), "[]"), " ", len(nums))
fmt.Printf("%q\n", sNums2)
sNums3 := strings.SplitN(strings.TrimRight(fmt.Sprint(nums)[1:], "]"), " ", len(nums))
fmt.Printf("%q\n", sNums3)
This will properly output (try it on the Go Playground):
[]
[]
[]
One thing to note here: the presented solutions are kind of like generic, because the solutions work with other types of slices, e.g. []int8
, []int32
, []float32
etc., even with arrays, e.g. [4]int
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With