cpfcnpj

package module
v0.0.0-...-a595c5f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2020 License: Apache-2.0 Imports: 4 Imported by: 10

README

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

Apache 2.0

Documentation

Overview

Package cpfcnpj provides functions for validate CPF and CNPJ, the Brazilian taxpayer registry identification document

Index

Constants

View Source
const (
	// CNPJFormatPattern is the pattern for string replacement
	// with Regex
	CNPJFormatPattern string = `([\d]{2})([\d]{3})([\d]{3})([\d]{4})([\d]{2})`
)
View Source
const (
	// CPFFormatPattern is the pattern for string replacement
	// with Regex
	CPFFormatPattern string = `([\d]{3})([\d]{3})([\d]{3})([\d]{2})`
)

Variables

This section is empty.

Functions

func Clean

func Clean(s string) string

Clean can be used for cleaning formatted documents

func ValidateCNPJ

func ValidateCNPJ(cnpj string) bool

ValidateCNPJ validates a CNPJ document You should use without punctuation

func ValidateCPF

func ValidateCPF(cpf string) bool

ValidateCPF validates a CPF document You should use without punctuation

Types

type CNPJ

type CNPJ string

CNPJ type definition

func NewCNPJ

func NewCNPJ(s string) CNPJ

NewCNPJ is a helper function to convert and clean a new CNPJ from a string

func (*CNPJ) IsValid

func (c *CNPJ) IsValid() bool

IsValid returns if CNPJ is a valid CNPJ document

func (*CNPJ) String

func (c *CNPJ) String() string

String returns a formatted CNPJ document 00.000.000/0001-00

type CPF

type CPF string

CPF type definition

func NewCPF

func NewCPF(s string) CPF

NewCPF is a helper function to convert and clean a new CPF from a string

func (*CPF) IsValid

func (c *CPF) IsValid() bool

IsValid returns if CPF is a valid CPF document

func (*CPF) String

func (c *CPF) String() string

String returns a formatted CPF document 000.000.000-00

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL