CPF and CNPJ - Validation package in Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
README.md
cnpj.go
cnpj_test.go
cpf.go
cpf_test.go
utils.go
utils_test.go

README.md

cpfcnpj

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.

Information about CPF and CNPJ

Installation

Use the go tool for do that:

$ go get github.com/klassmann/cpfcnpj

Usage

You need to import the package github.com/klassmann/cpfcnpj like that:

import (
    "github.com/klassmann/cpfcnpj"
)

Example

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
}

Function: cpfcnpj.ValidateCPF(s string) bool

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.")
    }
}

Function: cpfcnpj.ValidateCNPJ(s string) bool

Validates the CNPJ document

/// CNPJ
func ValidatingCNPJ() {
    r := cpfcnpj.ValidateCNPJ("64493884000146")

    if !r {
        fmt.Printf("The document is invalid.")
    }
}

Function: cpfcnpj.Clean(s string) string

Clean the formatted document

cpf := cpfcnpj.Clean("111.222.333-99")
cnpj := cpfcnpj.Clean("10.963.268/0001-82")

License

License