cnpj

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2021 License: BSD-3-Clause Imports: 4 Imported by: 6

README

Go CNPJ TestsGoDocGo version

A Go module to validate CNPJ numbers (Brazilian companies unique identifier for the Federal Revenue).

package main

import "github.com/cuducos/go-cnpj"


func main() {
	// these return true
	cnpj.IsValid("11222333000181")
	cnpj.IsValid("11.222.333/0001-81")

	// these return false
	cnpj.IsValid("11.111.111/1111-11")
	cnpj.IsValid("12.345.678 9012-34")
	cnpj.IsValid("AB.CDE.FGH/IJKL-MN")
	cnpj.IsValid("123")

	// this returns 11111111111111
	cnpj.Unmask("11.111.111/1111-11")

	// this returns 11.111.111/1111-11
	cnpj.Mask("11111111111111")
}

Based on Go CPF ❤️

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base added in v0.1.1

func Base(n string) string

Base returns the first 8 digits of the CNPJ number

Example (Invalid)
fmt.Println(Base("123"))
Output:

Example (ValidMasked)
fmt.Println(Base("11.222.333/0001-81"))
Output:

11222333
Example (ValidUnmasked)
fmt.Println(Base("11222333000181"))
Output:

11222333

func CheckDigit added in v0.1.1

func CheckDigit(n string) string

CheckDigit returns the last 2 digits of the CNPJ number.

Example (Invalid)
fmt.Println(CheckDigit("123"))
Output:

Example (ValidMasked)
fmt.Println(CheckDigit("11.222.333/0001-81"))
Output:

81
Example (ValidUnmasked)
fmt.Println(CheckDigit("11222333000181"))
Output:

81

func IsValid

func IsValid(n string) bool

IsValid checks whether CNPJ number is valid or not

Example (Invalid)
fmt.Println(IsValid("11.111.111/1111-11"))
Output:

false
Example (ValidMasked)
fmt.Println(IsValid("11.222.333/0001-81"))
Output:

true
Example (ValidUnmasked)
fmt.Println(IsValid("11222333000181"))
Output:

true

func Mask

func Mask(n string) string

Mask returns the CNPJ number formatted

Example (Invalid)
fmt.Println(Mask("42"))
Output:

42
Example (Valid)
fmt.Println(Mask("11111111111111"))
Output:

11.111.111/1111-11

func Order added in v0.1.1

func Order(n string) string

Order returns the 9th, 10th, 11th and 12th digits of the CNPJ number.

Example (Invalid)
fmt.Println(Order("123"))
Output:

Example (ValidMasked)
fmt.Println(Order("11.222.333/0001-81"))
Output:

0001
Example (ValidUnmasked)
fmt.Println(Order("11222333000181"))
Output:

0001

func Unmask

func Unmask(n string) string

Unmask removes any non-digit (numeric) from the CNPJ number

Example
fmt.Println(Unmask("11.111.111/1111-11"))
Output:

11111111111111

Types

This section is empty.

Jump to

Keyboard shortcuts

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