Files
  • main.go
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {
	/*
	   For details http://www.golangpro.com/2015/05/validate-indian-pan-using-go-regular-expression.html
	*/

	fmt.Println("Enter PAN code")
	var pan string
	fmt.Scanf("%s", &pan)
	//The string entered is converted to upper case
	pan = strings.ToUpper(pan)
	fmt.Println("You entered: ", pan)
	/*
	   Regular Expression
	   1. import regexp package
	   2. ^ indicates "at the beginning of string"
	   3. $ indicates "at the end of string"
	   4. [A-Z] indicates any upper case alphabet from A to Z both inclusive
	   5. {3} exactly 3
	   6. [0-9] indiates any numeric between 0 & 9 both inclusive
	*/

	re := regexp.MustCompile("^[A-Z]{3}[ABCFGHLJPT]{1}[A-Z]{1}[0-9]{4}[A-Z]{1}$")

	var t, msg string

	//If the pattern doesn't match MatchString will return "false"

	if !re.MatchString(pan) {
		msg = "PAN format is invalid"

	} else {

		//It's a valid PAN, Let us check the 4th character to find Type of Holder
		panType := pan[3:4]

		switch panType {
		case "A":
			t = "Association of Persons (AOP)"
		case "B":
			t = "Body of Individuals (BOI)"
		case "C":
			t = "Company"
		case "F":
			t = "Firm"
		case "G":
			t = "Government"
		case "H":
			t = "HUF (Hindu Undivided Family)"
		case "L":
			t = "Local Authority"
		case "J":
			t = "Artificial Judicial Person"
		case "P":
			t = "Individual"
		case "T":
			t = "AOP (Trust)"
		}
		fmt.Println("PAN is valid; type of holder of PAN card: ", t)
	}
	fmt.Println(msg)
}
go version go1.9.4 linux/amd64