To get a sample of 5 elements from an array in Go, you can use the math/rand
package to generate 5 random indices and then select the values at those indices from the array.
Here's an example code snippet:
main.go408 chars23 lines
In this example, we create an array of 10 integers and a slice of integers to hold our sample of 5 elements. We then loop 5 times, generating a random index within the bounds of the array and selecting the value at that index to add to our sample. Finally, we print out the resulting sample.
gistlibby LogSnag