CheckPasswordComplexity validates if a password meets the complexity requirements.
The password must contain at least 8 characters and include at least one number, one lowercase letter, one uppercase letter, and one symbol. It returns an error if the password does not meet these requirements.
Parameters:
- password: string, the password to be validated.
 
Returns:
- error: an error if the password does not meet the complexity requirements, or nil if the password meets the requirements.
 
func CheckPasswordComplexity(password string) error {
	// Check if the password is at least 8 characters long
	if len(password) < 8 {
		return fmt.Errorf("password should contain at least 8 characters")
	}
	// Check if the password contains at least one number
	numRegex := regexp.MustCompile(`\d`)
	if !numRegex.MatchString(password) {
		return fmt.Errorf("password needs a number")
	}
	// Check if the password contains at least one lowercase letter
	lowercaseRegex := regexp.MustCompile(`[a-z]`)
	if !lowercaseRegex.MatchString(password) {
		return fmt.Errorf("password needs a lowercase letter")
	}
	// Check if the password contains at least one uppercase letter
	uppercaseRegex := regexp.MustCompile(`[A-Z]`)
	if !uppercaseRegex.MatchString(password) {
		return fmt.Errorf("password needs an uppercase letter")
	}
	// Check if the password contains at least one symbol
	symbolRegex := regexp.MustCompile(`[!@#~$%^&*()+|_]`)
	if !symbolRegex.MatchString(password) {
		return fmt.Errorf("password needs a symbol")
	}
	// If all the requirements are met, return nil
	return nil
}
Experience the magic of coding unfold as you execute this code!
package main
import (
	"fmt"
	"regexp"
)
func CheckPasswordComplexity(password string) error {
	if len(password) < 8 {
		return fmt.Errorf("password should contain at least 8 characters")
	}
	numRegex := regexp.MustCompile(`\d`)
	if !numRegex.MatchString(password) {
		return fmt.Errorf("password needs a number")
	}
	lowercaseRegex := regexp.MustCompile(`[a-z]`)
	if !lowercaseRegex.MatchString(password) {
		return fmt.Errorf("password needs a lowercase letter")
	}
	uppercaseRegex := regexp.MustCompile(`[A-Z]`)
	if !uppercaseRegex.MatchString(password) {
		return fmt.Errorf("password needs an uppercase letter")
	}
	symbolRegex := regexp.MustCompile(`[!@#~$%^&*()+|_]`)
	if !symbolRegex.MatchString(password) {
		return fmt.Errorf("password needs a symbol")
	}
	return nil
}
func main() {
	fmt.Println(CheckPasswordComplexity("secret123"))   // password needs an uppercase letter
	fmt.Println(CheckPasswordComplexity("Secret123"))   // password needs a symbol
	fmt.Println(CheckPasswordComplexity("Secret123!!")) // <nil> -> password is complex
}
enjoy your code!