You can use the math/rand
package to generate random numbers. Here's an example of how you can generate a random number between 1 and 10:
main.go312 chars18 lines
In this code, we first seed the random number generator using the current timestamp to ensure that we get different results each time the program runs. We then generate a random number using rand.Intn(10)
, which returns a number between 0 and 9. We add 1 to this result to get a number between 1 and 10. Finally, we print the random number to the console.
gistlibby LogSnag