Last Updated: November 23, 2016
·
1.543K
· martinusso

Checks for Palindrome strings or numbers in Go Lang

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, ""))
}