const
 charset ="abcdefghijklmnopqrstuvwxyz"+"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

Then we create theStrings()function and callStringWithCharset()in it.

func String (length int) string{
     return StringWithCharset(length, charset)
}

And we are done. Just in case you need it, here is what your final code should look like.

package rand

import (  
  "math/rand"
  "time"
)

const charset = "abcdefghijklmnopqrstuvwxyz" +  
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

var seededRand *rand.Rand = rand.New(  
  rand.NewSource(time.Now().UnixNano()))

func StringWithCharset(length int, charset string) string {  
  b := make([]byte, length)
  for i := range b {
    b[i] = charset[seededRand.Intn(len(charset))]
  }
  return string(b)
}

func String(length int) string {  
  return StringWithCharset(length, charset)
}

results matching ""

    No results matching ""