Brazilian taxpayer identification (CPF e CNPJ) - validation package in Golang.
It is an essential package for include validation for these numbers in your application.
Eg: It can be used in a payment system or account system.
Use the go tool
for do that:
$ go get github.com/klassmann/cpfcnpj
You need to import the package github.com/klassmann/cpfcnpj
like that:
import (
"github.com/klassmann/cpfcnpj"
)
package main
import (
"fmt"
"github.com/klassmann/cpfcnpj"
)
func main() {
// This will initialize a new CNPJ and clear the string
cnpj := cpfcnpj.NewCNPJ("70.082.591/0001-79")
// Verifies if it is a valid document
if !cnpj.IsValid() {
panic(fmt.Errorf("it is not a valid document: %v", cnpj))
}
// Cleaned output
fmt.Printf("%v\n", cnpj)
// Output: 70082591000179
// Formatted output
fmt.Println(cnpj.String())
// Output: 70.082.591/0001-79
}
Validates the CPF document
import (
"fmt"
"github.com/klassmann/cpfcnpj"
)
/// CPF
func ValidatingCPF() {
r := cpfcnpj.ValidateCPF("71656686734")
if !r {
fmt.Printf("The document is invalid.")
}
}
Validates the CNPJ document
/// CNPJ
func ValidatingCNPJ() {
r := cpfcnpj.ValidateCNPJ("64493884000146")
if !r {
fmt.Printf("The document is invalid.")
}
}
Clean the formatted document
cpf := cpfcnpj.Clean("111.222.333-99")
cnpj := cpfcnpj.Clean("10.963.268/0001-82")