Go language to verify the validity of credit card number by Luhn algorithm _golang

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.