Learning

Using the Go programming language on this site: the camt and pain readers

November 18, 2026

This note is a reading of that code for someone new to go. I would recomend a visit of Tour of Go which is highly recommended across the internet as an introduction.

As for what I have done here, the software page there are two tools that are using go. They both read an ISO 20022 file in Go: a camt.053 bank statement, and a pain.001 credit transfer.

Both pages are themselves their own programs, they have their own set of source files that are combined in a package declaration that has its own entry point.

The examples are two programs, not two files. Each program is a package main with the same layout: a request handler, a parser, and tests. They read different ISO 20022 XML message types, but the roles of the Go files are alike.

Where the code is

Both function directories use the same file layout:

File What it does
main.go Starts the program and handles HTTP requests. It decodes the request body, calls the parser, and writes the HTTP response.
parse.go Defines the XML and JSON data structures, decodes and validates the message, then maps it to the data the page displays.
parse_test.go Tests the parser, including parsing the sample XML and rejecting invalid input.
go.mod Declares the program's Go module path and dependencies.
go.sum Records checksums for dependency versions.

The statement reader uses these files under netlify/functions/camt/; its module is hostlund.com/camt. Its parser handles camt.053:

The payment reader has the same files under netlify/functions/pain/; its module is hostlund.com/pain.

What each program does

CAMT

parseCamt in netlify/functions/camt/parse.go decodes a bank-to-customer statement. The document element it maps is BkToCstmrStmt/Stmt. For each statement it builds a statementView: an id, a creation time, the account (IBAN, or the other account id when there is no IBAN, plus currency and name), the balances, and the entries. Balance codes such as OPBD and CLBD become labels ("Opening booked", "Closing booked") in balanceLabel. Credit and debit indicators CRDT and DBIT become "Credit" and "Debit" in direction.

An entry can contain transaction details. entryViews flattens NtryDtls/TxDtls. When those details exist, the amount prefers the transaction amount, then the detail amount, then the entry amount. The counterparty depends on the direction: a debit's other party is the creditor, and a credit's other party is the debtor, falling back to the creditor when the debtor name is empty. That logic is counterparty. When an entry has no transaction details, the view still has the entry amount, dates, status, servicer reference, and additional info.

The group header of the sample statement (GrpHdr, message id SAMPLE-2026-02) is not mapped. encoding/xml skips elements that no struct field asks for.

PAIN

parsePain in netlify/functions/pain/parse.go decodes a customer credit transfer initiation, CstmrCdtTrfInitn. The JSON is one paymentView: the group header (message id, created time, initiator, count, control sum) and a list of payment blocks. Each block has the debtor, the debtor IBAN (or the other account id), the debtor BIC (BICFI, or the older BIC tag when BICFI is empty), the execution date, and the transfers. A transfer's account is the creditor IBAN, or the other account id. Remittance is the unstructured Ustrd lines joined with spaces.

A few parsed fields are not copied onto the JSON. On a transfer, InstrId and the creditor BIC are unmarshaled and then left off transferView. The page does not show them.

Both parsers reject an empty body and a document that does not have the expected shape. camt returns "no statement was found in the file" when no Stmt was mapped. pain returns "no payment was found in the file" when the message id is empty and there are no payment blocks.

How they are exposed

The software page links to /camt/ and /pain/, and describes the two readers in a sentence each.

src/pages/camt.js posts the XML to /api/camt and renders statements: the account line, a balance table (label, date, creditDebit, amount, currency), and an entry table (bookingDate, valueDate, creditDebit, amount, counterparty, endToEndId or reference, remittance).

src/pages/pain.js posts to /api/pain and renders the payment object itself: initiator, message id, count, control sum, and each block's debtor, IBAN, BIC, execution date, and transfers.

Both pages put the message in a text box. Load example fills that box with the sample. The page says the message is not stored. Nothing in the Go writes the body to disk or to a database. The response sets cache-control: no-store.

How a request is handled

The shape of main.go is the same in both programs.

handler accepts only POST. Anything else is HTTP 405. requestBody base64-decodes the body when IsBase64Encoded is set, which is the API Gateway proxy behaviour, and otherwise uses the body as bytes. evaluate rejects a body over maxBytes (1000000) with HTTP 413, calls the parser, and on success returns 200. camt wraps the slice as {"statements": ...}. pain marshals the paymentView as the whole body. A parser error becomes HTTP 400 with {"error": "..."}.

main has two modes. The statement program reads stdin when CAMT_DEV is stdin. The payment program does the same when PAIN_DEV is stdin. In that mode it prints the status code, a newline, and the JSON. Otherwise it calls lambda.Start(handler).

In gatsby develop, gatsby-node.js mounts POST /api/camt and POST /api/pain. Each mount runs go run . in the function directory with that environment variable set to stdin, passes the request body as the process input, and splits stdout on the first newline into a status and a payload. The timeout is 60 seconds. If the process fails or prints no newline, the dev server responds 500 with {"error":"The file could not be read."}.

On the deployed site, Netlify routes requests to /api/camt or /api/pain to the corresponding Go function. The Go main function starts its handler; main.go itself is not a URL endpoint. The redirect rules in netlify.toml map /api/camt to /.netlify/functions/camt and /api/pain to /.netlify/functions/pain. Their status = 200 makes these rewrites: the function handles the request while the browser continues using the /api/... URL. force = true applies each rule even if a file happens to exist at that path.

The [build] section in netlify.toml runs npm install && npm run build and publishes public for the Gatsby site. That command does not compile the Go programs. Netlify separately discovers and builds functions from the conventional netlify/functions directory, where camt and pain each have their own go.mod. The [build.environment] section sets build-time values such as the Node.js version and GATSBY_CPU_COUNT; it does not set CAMT_DEV or PAIN_DEV. With those variables unset in deployment, each Go main takes the lambda.Start(handler) path. The remaining [[headers]] rules set cache and content-type headers for parking pages and their manifest; they do not configure the Go functions.

Idioms, for a Java developer

One package, several files. Every file says package main. All of the functions and types in the directory are in one package, and main is the entry point, the same role as public static void main. There is no class wrapping them. Java would have made parseCamt a method on a class. Go lets the function sit at package scope.

Visibility is the capital letter. parseCamt, handler, and statement start with a lowercase letter, so another module cannot call them. The tests are package main, not package main_test, so they can call parseCamt directly. A Java test in the same package can call a package-private method for the same reason. The module path in go.mod is the import path, closest to Maven coordinates.

Errors are values. parseCamt returns ([]statementView, error). The caller writes if err != nil. There is no throw and no checked exception. fmt.Errorf builds the error. HTTP status is not the Go error return. handler almost always returns a nil error and puts 400, 405, or 413 on the response. The error return is the function runtime's failure channel. A nil error with a 400 response means "the invocation succeeded, the file did not."

Structs instead of beans. A struct is a block of fields. Go does not provide a default constructor or require a constructor call: declaring a variable gives it the type's zero value, and paymentView{} explicitly creates that zero-valued struct. On failure, pain returns paymentView{}, whose fields are "", nil, or 0. These files define no constructor functions. toView fills a struct with a composite literal, which resembles a constructor call with named fields. The .go files in each directory are compiled together as one package; when the program starts, Go initializes package-level variables and then calls main. The structs are created as the code runs, for example while parsing a request—not loaded from the source files as pre-made objects.

Slices instead of ArrayList. views := make([]statementView, 0, len(doc.Statements)) allocates a slice with length 0 and enough capacity for the statements, then append adds to it. The capacity argument is the same idea as new ArrayList<>(n). append may allocate a larger array if the capacity is exceeded. flatten starts from a nil slice and appends. A nil slice marshals as JSON null if it is ever returned directly. The views in this code are built with make, so an empty list of balances or transfers marshals as [].

The interface is implicit. localNames does not declare that it implements a type. xml.NewTokenDecoder asks for a TokenReader, which is an interface with one method, Token() (Token, error). Having the method is enough. Java would say implements TokenReader. The method has a value receiver, func (l localNames) Token(), and the decoder pointer lives inside the struct, so the copy still reads the same underlying decoder. The loop for i := range element.Attr uses the index because a range value is a copy. Writing through the index updates the attribute that is returned. That is a common Go slip, and this code avoids it.

switch does not fall through. direction and balanceLabel switch on the upper-cased code and return from each case. There is no break. A case ends at the next case.

Variadic helpers. firstText(values ...string) is String... in Java. It returns the first value that is not blank after strings.TrimSpace. Dates use it so a date-only tag wins when it is present and a date-time tag is the fallback: firstText(item.BookingDate, item.BookingTime).

any. map[string]any{"statements": statements} is a map from string to an arbitrary value, closest to Map<String, Object>. any is a predeclared alias for interface{}. This code does not use type parameters beyond that.

Short declarations. stmt := statements[0] declares and assigns. The type comes from the right-hand side, the same information var carries in Java, written at the call.

Tests. parse_test.go imports the testing package. t.Fatal fails the test and stops it, in the role of an assertion failure. There is no test class and no framework in these files. go test finds the files by the _test.go suffix.

What this code does not use

There are no goroutines, no channels, and no mutexes. Concurrency is not a reason this program is in Go. The reasons visible in the code are the small runtime, the XML and JSON libraries, a single binary, and the function entry point.

There is no shared internal package. The duplication of localNames, amount, firstText, and cleanList is the cost of that.

All notes