Skip to content

Instantly share code, notes, and snippets.

@ozkansen
Created October 19, 2021 22:21
Show Gist options
  • Save ozkansen/e3823af2679f17cfd9b648987279b606 to your computer and use it in GitHub Desktop.
Save ozkansen/e3823af2679f17cfd9b648987279b606 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
var g GID
fmt.Println(g)
}
type GID string
var GIDNumbers = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
func NewRandomGID() GID {
rand.Seed(time.Now().UnixNano())
var gid string
for len(gid) < 16 {
numbers_lenght := len(GIDNumbers)
x := rand.Intn(numbers_lenght)
gid += GIDNumbers[x]
}
return GID(gid)
}
func StringToGID(n string) (GID, bool) {
data := GID(n)
check := CheckGID(data)
return data, check
}
func CheckGID(n GID) bool {
// lenght check
if len(n) != 16 {
return false
}
// in list control
for _, n_value := range n {
check := false
for _, g_value := range GIDNumbers {
if string(n_value) == g_value {
check = true
break
}
}
if check != true {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment