To generate a random string in Go, we can use the crypto/rand package. Here's an example code that generates a random string of length n:
main.go178 chars11 linesIn the above code, we're creating a byte slice of length n using the make function. Then we're using the rand.Read function to fill this slice with random bytes generated by the crypto/rand package. Finally, we're encoding this byte slice as a URL-safe base64 string using the base64.URLEncoding.EncodeToString function and returning it.
To generate a random string of length 10, we can call the randomString function like this:
main.go30 chars2 lines
gistlibby LogSnag