···11# Go CNPJ []()[](https://godoc.org/github.com/cuducos/go-cnpj)
2233-A Go module to validate CNPJ numbers (Brazilian companies unique identifier for the Federal Revenue).
33+A Go module to validate CNPJ numbers (Brazilian companies' unique identifier for the Federal Revenue).
44+55+> [!IMPORTANT]
66+> Starting in July 2026 [the CNPJ number will be alphanumeric](https://www.gov.br/receitafederal/pt-br/acesso-a-informacao/acoes-e-programas/programas-e-atividades/cnpj-alfanumerico). This package **already supports the new format**. If you **do not** want to support the new format, tag this package to [`v0.1.1`](https://github.com/cuducos/go-cnpj/releases/tag/v0.1.1).
4758```go
69package main
···1215 // these return true
1316 cnpj.IsValid("11222333000181")
1417 cnpj.IsValid("11.222.333/0001-81")
1818+ cnpj.IsValid("12.ABC.345/01DE-35")
1919+ cnpj.IsValid("12ABC34501DE35")
15201621 // these return false
1722 cnpj.IsValid("11.111.111/1111-11")
···1924 cnpj.IsValid("AB.CDE.FGH/IJKL-MN")
2025 cnpj.IsValid("123")
21262222- // this returns 11111111111111
2727+ // these return 11111111111111 and 12ABC34501DE35
2328 cnpj.Unmask("11.111.111/1111-11")
2929+ cnpj.Unmask("12.ABC.345/01DE-35")
24302525- // this returns 11.111.111/1111-11
3131+ // these return 11.111.111/1111-11 and 12.ABC.345/01DE-35
2632 cnpj.Mask("11111111111111")
3333+ cnpj.Mask("12ABC34501DE35")
2734}
2835```
2936
+14-20
cnpj.go
···33import (
44 "fmt"
55 "regexp"
66- "strconv"
77- "strings"
86)
971010-func checksum(ds []int64, ref []int64) int64 {
1111- var s int64
88+func checksum(ds []int32, ref []int32) int32 {
99+ var s int32
1210 for i, n := range ref {
1311 s += n * ds[i]
1412 }
···1917 return 11 - r
2018}
21192222-//IsValid checks whether CNPJ number is valid or not
2020+// IsValid checks whether CNPJ number is valid or not
2321func IsValid(n string) bool {
2422 u := Unmask(n)
2523 if len(u) != 14 {
2624 return false
2725 }
28262929- ds := make([]int64, 14)
3030- for i, v := range strings.Split(u, "") {
3131- c, err := strconv.ParseInt(v, 10, 32)
3232- if err != nil {
3333- return false
3434- }
3535- ds[i] = c
2727+ ds := make([]int32, 14)
2828+ for i, v := range []rune(u) {
2929+ ds[i] = int32(v) - 48 // ASCII value minus 48 so "0" becomes 0 and so on
3630 }
37313838- r1 := []int64{5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
3939- r2 := []int64{6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
3232+ r1 := []int32{5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
3333+ r2 := []int32{6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
4034 return checksum(ds, r1) == ds[12] && checksum(ds, r2) == ds[13]
4135}
42364343-//Mask returns the CNPJ number formatted
3737+// Mask returns the CNPJ number formatted
4438func Mask(n string) string {
4539 u := Unmask(n)
4640 if len(u) != 14 {
···4943 return fmt.Sprintf("%s.%s.%s/%s-%s", u[:2], u[2:5], u[5:8], u[8:12], u[12:])
5044}
51455252-//Unmask removes any non-digit (numeric) from the CNPJ number
4646+// Unmask removes any non-digit (numeric) from the CNPJ number
5347func Unmask(n string) string {
5454- return regexp.MustCompile(`\D`).ReplaceAllString(n, "")
4848+ return regexp.MustCompile(`[^0-9A-Z]`).ReplaceAllString(n, "")
5549}
56505757-//Base returns the first 8 digits of the CNPJ number
5151+// Base returns the first 8 digits of the CNPJ number
5852func Base(n string) string {
5953 if !IsValid(n) {
6054 return ""
···6357 return Unmask(n)[0:8]
6458}
65596666-//Order returns the 9th, 10th, 11th and 12th digits of the CNPJ number.
6060+// Order returns the 9th, 10th, 11th and 12th digits of the CNPJ number.
6761func Order(n string) string {
6862 if !IsValid(n) {
6963 return ""
···7266 return Unmask(n)[8:12]
7367}
74687575-//CheckDigit returns the last 2 digits of the CNPJ number.
6969+// CheckDigit returns the last 2 digits of the CNPJ number.
7670func CheckDigit(n string) string {
7771 if !IsValid(n) {
7872 return ""