Share & grow the world's code base!

Delve into a community where programmers unite to discover code snippets, exchange skills, and enhance their programming proficiency. With abundant resources and a supportive community, you'll find everything essential for your growth and success.

1 snippets
  • Generate random hash or token in Go

    package main
    import (
    func GenerateRandomHash(n int) (string, error) {
    	b := make([]byte, n)
    	_, err := rand.Read(b)
    	// Note that err == nil only if we read len(b) bytes.
    	if err != nil {
    		return "", err
    	hasher := sha256.New()
    	sha := hex.EncodeToString(hasher.Sum(nil))
    	return sha, nil
    func main() {
    	hash, err := GenerateRandomHash(512)
    	if err != nil {
    // go run main.go 
    // 34c0fb393623843e56719b5d9d66385a55b4b4d3393187b7b1a76aee46c421c5

    Here is an example of randomly generating a hash or token in Go.