In Go, you can create a range of integers using the range
keyword. You can use this range with a for
loop to iterate over the range of numbers.
Here's an example of how to create a range of numbers in Go:
main.go213 chars14 lines
In this example, we've created a slice of integers containing the values 1 to 5. We then loop through this slice using a for
loop with the range
keyword, which returns the index and value of each element in the slice.
The output of this program will be:
main.go10 chars6 lines
You can also create a range of numbers using the make
function, like this:
main.go261 chars17 lines
In this example, we've created a slice of integers with a length of 5 using the make
function. We then fill this slice with the values from 1 to 5 using a for
loop with the range
keyword.
The output of this program will be the same as the previous example:
main.go10 chars6 lines
gistlibby LogSnag