Skip to content

Instantly share code, notes, and snippets.

@emmiep
Created April 6, 2019 18:56
Show Gist options
  • Select an option

  • Save emmiep/ad938aa59a18431008e1f94bbcb2df2a to your computer and use it in GitHub Desktop.

Select an option

Save emmiep/ad938aa59a18431008e1f94bbcb2df2a to your computer and use it in GitHub Desktop.
Random strings in Go
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
)
func randomString(n int) (str string, err error) {
buf := make([]byte, n)
if _, err = io.ReadFull(rand.Reader, buf); err != nil {
return
}
str = hex.EncodeToString(buf)
return
}
func main() {
if str, err := randomString(20); err != nil {
panic(err)
} else {
fmt.Println(str)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment