Skip to content

Instantly share code, notes, and snippets.

@ikasamt
Created October 24, 2021 23:50
Show Gist options
  • Save ikasamt/0e0090026e33cdf94775c071bdc652d9 to your computer and use it in GitHub Desktop.
Save ikasamt/0e0090026e33cdf94775c071bdc652d9 to your computer and use it in GitHub Desktop.
ランダム文字列の生成方法
package main
import (
"crypto/md5"
"encoding/hex"
"log"
"math/rand"
"time"
"github.com/google/uuid"
)
const RandomChars = "abcdefghijklmnopqrstuvwxyz0123456789"
func RandomStr(size int) (result string) {
for i := 0; i < size; i++ {
rand.Seed(time.Now().UnixNano())
randPos := rand.Intn(len(RandomChars) - 1)
result += RandomChars[randPos : randPos+1]
}
return
}
func GetMd5(text string) string {
hash := md5.New()
defer hash.Reset()
hash.Write([]byte(text))
return hex.EncodeToString(hash.Sum(nil))
}
func main() {
// ランダム文字列
log.Println(RandomStr(100))
// uuid
uuidObj, _ := uuid.NewUUID()
log.Println(uuidObj.String())
// uuidのmd5
log.Println(GetMd5(uuidObj.String()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment