In Go, you can convert a set of integers to a set of strings by iterating over each element of the integer set and converting it to a string using the strconv.Itoa()
function.
In this example,
intSet1
containing the elements 1, 2, 3, 4, 5
.stringSet1
.intSet1
using a for range
loop.strconv.Itoa()
function and add it to the stringSet1
.stringSet1
to standard output.package main
import (
"fmt"
"strconv"
)
func main() {
intSet1 := map[int]struct{}{1: {}, 2: {}, 3: {}, 4: {}, 5: {}}
stringSet1 := map[string]struct{}{}
for num := range intSet1 {
str := strconv.Itoa(num)
stringSet1[str] = struct{}{}
}
fmt.Println(stringSet1)
}
map[1:{} 2:{} 3:{} 4:{} 5:{}]
In this example,
intSet2
containing the elements 10, 20, 30, 40, 50
.stringSet2
.intSet2
using a for range
loop.strconv.Itoa()
function and add it to the stringSet2
.stringSet2
to standard output.package main
import (
"fmt"
"strconv"
)
func main() {
intSet2 := map[int]struct{}{10: {}, 20: {}, 30: {}, 40: {}, 50: {}}
stringSet2 := map[string]struct{}{}
for num := range intSet2 {
str := strconv.Itoa(num)
stringSet2[str] = struct{}{}
}
fmt.Println(stringSet2)
}
map[10:{} 20:{} 30:{} 40:{} 50:{}]
In this tutorial, we learned How to Convert a Set of Integers to a Set of Strings in Go language with well detailed examples.