README
ΒΆ
go-boleto-utils
π Overview
go-boleto-utils is a comprehensive Go library designed to simplify working with Brazilian bank slips (boletos). This utility package provides robust parsing and validation functionalities for digitable lines and barcodes, making it easier for developers to integrate boleto-related operations into their Go applications.
π Features
- π Parse digitable lines and barcodes
- β Validate boleto integrity
- π¦ Identify bank and boleto types
- π’ Extract key financial details
π Prerequisites
- Go 1.16 or higher
- Basic understanding of Brazilian banking document structures
π Installation
Install the library using Go modules with the following commands:
go get -u github.com/fonini/go-boleto-utils/parser
go get -u github.com/fonini/go-boleto-utils/validator
π§° Usage Examples
1. Parsing a Digitable Line
package main
import (
"fmt"
"github.com/fonini/go-boleto-utils/parser"
)
func main() {
digitableLine := "34191.75124 34567.871230 41234.560005 8 92850000026035"
result, err := parser.Parse(digitableLine)
if err != nil {
fmt.Println("Error parsing the digitable line:", err)
return
}
fmt.Printf("Bank: %s (%s)\n", result.IssuerBankName, result.IssuerBankCode)
fmt.Printf("Amount: R$ %.2f\n", result.Amount)
fmt.Printf("Due Date: %s\n", result.DueDate.Format("2006-01-02"))
fmt.Printf("Code Type: %s\n", result.CodeType) // DIGITABLE_LINE
}
2. Using barcode
package main
import (
"fmt"
"github.com/fonini/go-boleto-utils/parser"
)
func main() {
barCode := "74898992100000845361121577703702280000282105"
result, err := parser.Parse(barCode)
if err != nil {
fmt.Println("Error parsing the barcode:", err)
return
}
fmt.Printf("Bank: %s (%s)\n", result.IssuerBankName, result.IssuerBankCode)
fmt.Printf("Amount: R$ %.2f\n", result.Amount)
fmt.Printf("Due Date: %s\n", result.DueDate.Format("2006-01-02"))
fmt.Printf("Code Type: %s\n", result.CodeType) // BARCODE
}
Parser Output Fields
IssuerBankCode: Numeric code of the issuing bankIssuerBankName: Name of the issuing bankCurrency: Monetary representation codeDueDate: Expiration date of the bank slipAmount: Total amount of the bank slipCodeType: Type of the input code (DIGITABLE_LINE, BARCODE or UNKNOWN)
3. Validating a Boleto
Quickly validate the integrity of a boleto's digitable line:
package main
import (
"fmt"
"github.com/fonini/go-boleto-utils/validator"
)
func main() {
digitableLine := "34191.75124 34567.871230 41234.560005 8 92850000026035"
if validator.Validate(digitableLine) {
fmt.Println("β
The boleto is valid")
} else {
fmt.Println("β The boleto is not valid")
}
}
π¬ Helper methods
GetBoletoType
Determines boleto type.
func GetBoletoType(code string) utils.BoletoType
Returns
CREDIT_CARDCITY_HALLSSANITATIONELECTRICITY_AND_GASGOVERNMENT_AGENCIESTELECOMMUNICATIONSPAYMENT_BOOKLETSTRAFFIC_FINESBANK
code := "00190500954014481606906809350314337370000000100"
boletoType := GetBoletoType(code)
fmt.Println(boletoType) // BANK
GetCodeType
Identifies payment code type (digitable line or barcode).
func GetCodeType(code string) (utils.BoletoCodeType, error)
Returns
DIGITABLE_LINEBARCODEUNKNOWN
code := "00190500954014481606906809350314337370000000100"
codeType, err := GetCodeType(code)
if err != nil {
fmt.Println("Error determining code type:", err)
} else {
fmt.Printf("The code type is: %s\n", codeType) // DIGITABLE_LINE
}
π§ͺ Testing
Run comprehensive tests using the following commands:
# Run all tests
go test ./...
# Run tests for a specific package
go test ./validator
# Run tests with verbose output
go test -v ./...
ποΈ Supported Banks
While the library aims to support multiple Brazilian banks, please check the documentation for the most up-to-date list of supported institutions.
π§ Limitations
- Focuses on parsing and validation
- Does not handle boleto payment or generation
- Requires well-formed digitable lines
π License
This project is licensed under the MIT License. See the LICENSE file for complete details.
π Contact
For issues, questions, or contributions, please open an issue on the GitHub repository.