cpf

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2020 License: GPL-3.0 Imports: 4 Imported by: 1

README

Go CPF TestsGoDocGo version

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

package main

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


func main() {
	// these return true
	cpf.IsValid("23858488135")
	cpf.IsValid("238.584.881-35")

	// these return false
	cpf.IsValid("111.111.111-11")
	cpf.IsValid("123.456.769/01")
	cpf.IsValid("ABC.DEF.GHI-JK")
	cpf.IsValid("123")

	// this returns 11111111111
	cpf.Unmask("111.111.111-11")

	// this returns 111.111.111-11
	cpf.Mask("11111111111")
}

I started to learn Go with Learn Go With Tests and this CPF (Brazilian unique identifier for the Federal Revenue) validation script is actually my very first lines in Go (except the ones from the book). I'm sharing it here to get feedback ❤️

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsValid

func IsValid(n string) bool

IsValid checks whether CPF number is valid or not

Example (Invalid)
fmt.Println(IsValid("111.111.111-11"))
Output:

false
Example (ValidMasked)
fmt.Println(IsValid("238.584.881-35"))
Output:

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

true

func Mask

func Mask(n string) string

Mask returns the CPF number formatted

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

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

111.111.111-11

func Unmask

func Unmask(n string) string

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

Example
fmt.Println(Unmask("111.111.111-11"))
Output:

11111111111

Types

This section is empty.

Jump to

Keyboard shortcuts

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