To create a set of numbers from 1 to N in Go, you can use a map where the keys represent the numbers.
In this example,
numberSet
.true
in the map.package main
import (
"fmt"
)
func main() {
numberSet := make(map[int]bool)
for i := 1; i <= 5; i++ {
numberSet[i] = true
}
fmt.Println("Set of numbers from 1 to 5:", numberSet)
}
Set of numbers from 1 to 5: map[1:true 2:true 3:true 4:true 5:true]
In this example,
numberSet
.true
in the map.package main
import (
"fmt"
)
func main() {
numberSet := make(map[int]bool)
for i := 1; i <= 10; i++ {
numberSet[i] = true
}
fmt.Println("Set of numbers from 1 to 10:", numberSet)
}
Set of numbers from 1 to 10: map[1:true 2:true 3:true 4:true 5:true 6:true 7:true 8:true 9:true 10:true]
In this tutorial, we learned How to create a Set of Numbers from 1 to N in Go language with well detailed examples.