go-实用方法-判断字符串为uuid类型

go 遇到需要判断字符串是否为uuid 类型,函数如下

1
2
3
4
func IsValidUUID(uuid string) bool {
r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
return r.MatchString(uuid)
}

推荐使用

1
2
3
4
import github.com/robin5911/gtools/str

isUuid := str.CheckLikeUUID("e451f79c-f4ab-4c3a-b895-288bc60c2c5b")
fmt.Println(isUuid)