This article illustrates how the go language verifies the validity of a credit card number through the Luhn algorithm. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Package Main
Import (
"FMT"
"Strings"
)
Const INPUT = ' 49927398716
49927398717
1234567812345678
1234567812345670 '
var t = [...] Int{0, 2, 4, 6, 8, 1, 3, 5, 7, 9}
Func Luhn (s string) bool {
Odd: = Len (s) & 1
var sum int
For I, c: = Range S {
If C < ' 0 ' | | C > ' 9 ' {
return False
}
if i&1 = = Odd {
Sum + + t[c-' 0 ']
} else {
sum + = Int (c-' 0 ')
}
}
return sum%10 = = 0
}
Func Main () {
For _, S: = Range strings. Split (input, "\ n") {
Fmt. Println (S, Luhn (s))
}
}
Output results
Copy Code code as follows:
49927398716 true
49927398717 false
1234567812345678 false
1234567812345670 true
I hope this article will help you with your go language program.