Skip to content

Instantly share code, notes, and snippets.

@linux4life798
Created July 10, 2018 18:08
Show Gist options
  • Save linux4life798/1dcc9181518e10a3fcc35c81e45a42b2 to your computer and use it in GitHub Desktop.
Save linux4life798/1dcc9181518e10a3fcc35c81e45a42b2 to your computer and use it in GitHub Desktop.
Function to generate a random MQTT client ID with a common prefix
import CRAND "crypto/rand"
// GenMQTTClientID generates a random client id for mqtt
func GenMQTTClientID(prefix string) (string, error) {
r, err := CRAND.Int(CRAND.Reader, new(big.Int).SetInt64(100000))
if err != nil {
return "", fmt.Errorf("Failed to generate MQTT client ID: %v", err)
}
return prefix + r.String(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment