makers.go 821 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. "crypto/rand"
  4. "encoding/json"
  5. "io/ioutil"
  6. "os"
  7. )
  8. func mkstring () string {
  9. stringchars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  10. newstring := ""
  11. b := make([]byte, 5)
  12. // 乱数を生成
  13. if _, err := rand.Read(b); err != nil {
  14. return "不明なエラー"
  15. }
  16. // ランダムに取り出して文字列を生成
  17. for _, v := range b {
  18. // index が stringchars の長さに収まるように調整
  19. newstring += string(stringchars[int(v)%len(stringchars)])
  20. }
  21. return newstring
  22. }
  23. func insertjson (url string, linkpath string) string {
  24. payload := getlinks(linkpath)
  25. newstring := mkstring()
  26. payload[newstring] = url
  27. m, _ := json.Marshal(&payload)
  28. payload = nil
  29. ioutil.WriteFile(linkpath, m, os.ModePerm)
  30. return newstring
  31. }