morsetrie is a small, lightning fast Morse code decoder built around a
compact trie data structure. It turns
sequences of dots and dashes into text by walking a pre-built decoding
tree, making decoding efficient and predictable even for long inputs.
The package ships with a static trie based on the International Telecommunication Union (ITU) M.1677 recommendation for International Morse code, which includes letters, digits, and common punctuation.
- Decodes
.and-into runes using a trie-based lookup. - Uses whitespace (
space,tab,newline,\r) to delimit characters. - Treats
/as a word separator and emits a space in the decoded output. - Represents unknown or invalid Morse sequences as
?(rather than failing mid-stream).
A trie is a natural fit for Morse: each dot/dash is a step down the tree. This avoids repeatedly scanning a table or building strings for lookups. Internally the implementation is array-backed and uses int16 child indices to keep memory usage low while remaining cache-friendly.
Refer to the package documentation on pkg.go.dev.
package main
import (
"fmt"
"github.com/pierow2k/morsetrie"
)
func main() {
// Define the Morse code string to decode.
morseCode := "- .... .. ... / .. ... / -- --- .-. ... . - .-. .. ."
// The Decode function will use the static trie to decode.
text, err := morsetrie.Decode(morseCode)
if err != nil {
panic(err)
}
// Print the decoded text.
fmt.Println(text)
}Output
THIS IS MORSETRIE
- If the input contains unsupported characters (anything other than
.,-,/, or whitespace), decoding fails withErrUnexpectedChar. - If the input contains a syntactically valid but unknown Morse sequence,
the decoder emits
?for that symbol and continues.
- Add a star to the morsetrie GitHub repository
- Have an idea for a new feature or noticed something that isn’t working quite right? Open an issue
- If you’ve made improvements or fixed a bug, submit a pull request
morsetrie is distributed under the MIT License. See the LICENSE file for more details.