Golang : Check if password length meet the requirement
Problem :
You need to check the password's length entered by users. How to do that in Golang?
Solution :
Use the builtin len()
function to evaluate the length of the password and return true if the password length is acceptable or false if not.
See example :
package main
import "fmt"
func checkLength(value string, min, max int) bool {
pwdLen := len(value)
if min > 0 && pwdLen < min {
return false
}
if max > 0 && pwdLen > max {
return false
}
return true
}
func main() {
password := "abc12"
passwordMin := 6
passwordMax := 20
if !checkLength(password, passwordMin, passwordMax) {
fmt.Println("Password needs to be more than 6 characters and less than 20 characters")
} else {
fmt.Println("Password length accepted")
}
password = "abc1234"
if !checkLength(password, passwordMin, passwordMax) {
fmt.Println("Password needs to be more than 6 characters and less than 20 characters")
} else {
fmt.Println("Password length accepted")
}
}
Sample output :
Password needs to be more than 6 characters and less than 20 characters
Password length accepted
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.3k Golang : Create matrix with Gonum Matrix package example
+12.2k Golang : Extract part of string with regular expression
+9.4k Javascript : Read/parse JSON data from HTTP response
+9.4k Golang : Copy map(hash table) example
+6.3k Grep : How to grep for strings inside binary data
+12.3k Golang : Exit, terminating or aborting a program
+4.3k Java : Generate multiplication table example
+9k Golang : Generate EAN barcode
+6.3k Golang : Map within a map example
+6.7k Golang : How to setup a disk space used monitoring service with Telegram bot
+8.8k Golang : Inject/embed Javascript before sending out to browser example
+5.1k Golang : Generate Interleaved 2 inch by 5 inch barcode