···11+name: Tests
22+33+on: [pull_request, push]
44+55+jobs:
66+77+ build:
88+ name: Build
99+ runs-on: ubuntu-latest
1010+ steps:
1111+1212+ - name: Set up Go 1.14
1313+ uses: actions/setup-go@v1
1414+ with:
1515+ go-version: 1.14
1616+ id: go
1717+1818+ - name: Check out code into the Go module directory
1919+ uses: actions/checkout@v2
2020+2121+ - name: Get dependencies
2222+ run: |
2323+ go get -v -t -d ./...
2424+ if [ -f Gopkg.toml ]; then
2525+ curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
2626+ dep ensure
2727+ fi
2828+2929+ - name: Build
3030+ run: go test
+12
LICENSE
···11+Copyright (c) 2020 Eduardo Cuducos
22+All rights reserved.
33+44+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
55+66+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
77+88+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
99+1010+3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
1111+1212+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+30
README.md
···11+# Go CNPJ []()[](https://godoc.org/github.com/cuducos/go-cnpj)
22+33+A Go module to validate CNPJ numbers (Brazilian companies unique identifier for the Federal Revenue).
44+55+```go
66+package main
77+88+import "github.com/cuducos/go-cnpj"
99+1010+1111+func main() {
1212+ // these return true
1313+ cnpj.IsValid("11222333000181")
1414+ cnpj.IsValid("11.222.333/0001-81")
1515+1616+ // these return false
1717+ cnpj.IsValid("11.111.111/1111-11")
1818+ cnpj.IsValid("12.345.678 9012-34")
1919+ cnpj.IsValid("AB.CDE.FGH/IJKL-MN")
2020+ cnpj.IsValid("123")
2121+2222+ // this returns 11111111111111
2323+ cnpj.Unmask("11.111.111/1111-11")
2424+2525+ // this returns 11.111.111/1111-11
2626+ cnpj.Mask("11111111111111")
2727+}
2828+```
2929+3030+Based on [Go CPF](https://github.com/cuducos/go-cpf) ❤️
+57
cnpj.go
···11+package cnpj
22+33+import (
44+ "fmt"
55+ "regexp"
66+ "strconv"
77+ "strings"
88+)
99+1010+1111+1212+func checksum(ds []int64, ref []int64) int64 {
1313+ var s int64
1414+ for i, n := range ref {
1515+ s += n * ds[i]
1616+ }
1717+ r := s % 11
1818+ if r < 2 {
1919+ return 0
2020+ }
2121+ return 11 - r
2222+}
2323+2424+//IsValid checks whether CNPJ number is valid or not
2525+func IsValid(n string) bool {
2626+ u := Unmask(n)
2727+ if len(u) != 14 {
2828+ return false
2929+ }
3030+3131+ ds := make([]int64, 14)
3232+ for i, v := range strings.Split(u, "") {
3333+ c, err := strconv.ParseInt(v, 10, 32)
3434+ if err != nil {
3535+ return false
3636+ }
3737+ ds[i] = c
3838+ }
3939+4040+ r1 := []int64{5,4,3,2,9,8,7,6,5,4,3,2}
4141+ r2 := []int64{6,5,4,3,2,9,8,7,6,5,4,3,2}
4242+ return checksum(ds, r1) == ds[12] && checksum(ds, r2) == ds[13]
4343+}
4444+4545+//Mask returns the CNPJ number formatted
4646+func Mask(n string) string {
4747+ u := Unmask(n)
4848+ if len(u) != 14 {
4949+ return n
5050+ }
5151+ return fmt.Sprintf("%s.%s.%s/%s-%s", u[:2], u[2:5], u[5:8], u[8:12], u[12:])
5252+}
5353+5454+//Unmask removes any non-digit (numeric) from the CNPJ number
5555+func Unmask(n string) string {
5656+ return regexp.MustCompile(`\D`).ReplaceAllString(n, "")
5757+}