简体   繁体   中英

Using the unicode package for validation in Go?

The unicode package in Go provides functions such as unicode.IsDigit and unicode.IsUpper. Are these safe to use for form input validation, password validation in particular?

How would you approach form validation in Go without using a third party package? From what I can gather regex is expensive and should be avoided if possible, is this a viable solution?

Here's an example using the unicode package:

https://play.golang.org/p/6XLSqPim54E

I wouldn't say that regex is expensive and should be avoided if possible. It is the best tool for your case. Although there is nothing wrong to use unicode package for password validation, it might be more readable for other programmers to just use single regexp, where you can write all your password requirements into a single regular expression.

Ad regex performance: You can read about golang regex implementation's performance here . I wouldn't care about it unless you're implementing a very critical performance heavy tool where you can especially measure that regex slows your program down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM