a6ghja
Last Updated: November 23, 2016
·
103
· martinusso
0c22ed10d238d09c59c2d897f444ad12

Checks for Palindrome strings or numbers in Go Lang

go
import (
    "regexp"
    "strings"
)

func IsPalindromic(value string) bool {
    value = sanitize(value)
    for i := 0; i < len(value)/2; i++ {
        if value[i] != value[len(value)-i-1] {
            return false
        }
    }
    return true
}

func sanitize(value string) string {
    reg, _ := regexp.Compile("[^A-Za-z0-9]+")
    safe := reg.ReplaceAllString(value, "")
    return strings.ToLower(strings.Trim(safe, ""))
}
Say Thanks
Respond