Skip to content

Instantly share code, notes, and snippets.

@ahmdrz
Created September 13, 2016 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmdrz/e08408bd2895f7e90fbec33d2cb1c43a to your computer and use it in GitHub Desktop.
Save ahmdrz/e08408bd2895f7e90fbec33d2cb1c43a to your computer and use it in GitHub Desktop.
تایید هویت کد ملی در زبان گو , Golang , کد ملی
package main
import (
"strconv"
)
func ValidateIranianCode(code string) bool {
ToInt := func(str string) int {
if icode, err := strconv.Atoi(str); err == nil {
return icode
}
return -1
}
if icode := ToInt(code); icode >= 0 {
if len(code) < 8 || icode == 0 {
return false
}
code = ("0000" + code)[len(code)+4-10:]
if jcode := ToInt(code[3:9]); jcode >= 0 {
if jcode == 0 {
return false
}
} else {
return false
}
if ccode := ToInt(code[9:10]); ccode >= 0 {
var scode = 0
for i := 0; i < 9; i++ {
temp := ToInt(code[i : i+1])
if temp < 0 {
return false
}
scode += temp * (10 - i)
}
scode = scode % 11
return (scode < 2 && ccode == scode) || (scode >= 2 && ccode == (11-scode))
} else {
return false
}
} else {
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment