Skip to content

encoding Module

The encoding standard library provides a wide range of encode/decode utilities:

  • Base64, Base64URL
  • Base32
  • Base58 (Bitcoin alphabet)
  • Hex
  • CSV
  • HTML escape/unescape
  • XML: basic escape/unescape, marshal/unmarshal, mixed content, and namespace-aware
  • UTF-8 helpers (compatibility wrappers; prefer the utf8 module for new code)
  • Binary endian helpers (LE/BE for int32/int64/float32/float64)
  • LEB128 (signed/unsigned)
  • txtar (minimal parser/writer)
  • iconv (character set conversion) and IconvEx (options)

Functions

  • Base64Encode(s string) (string, error)
  • Base64Decode(s string) (string, error)
  • Base64URLEncode(s string) (string, error)
  • Base64URLDecode(s string) (string, error)
  • Base32Encode(s string) (string, error)
  • Base32Decode(s string) (string, error)
  • HexEncode(s string) (string, error)
  • HexDecode(s string) (string, error)
  • CSVReadAll(text string) (array, error)
  • CSVWriteAll(rows array) (string, error)
  • CSVParse(text string, opts map?) (array, error)
  • HTMLEscape(s string) (string, error)
  • HTMLUnescape(s string) (string, error)
  • XMLEscape(s string) (string, error)
  • XMLUnescape(s string) (string, error)
  • XMLUnmarshal(xml string) (node map, error)
  • XMLMarshal(node map, header bool?) (string, error)
  • XMLUnmarshalMixed(xml string) (node map, error)
  • XMLMarshalMixed(node map, header bool?) (string, error)
  • XMLUnmarshalNS(xml string) (node map, error)
  • XMLMarshalNS(node map, header bool?) (string, error)
  • UTF8RuneCount(s string) (int, error)
  • UTF8Valid(s string) (bool, error)
  • UTF8EastAsianWidth(s string) (int, error)

Note: These UTF-8 helpers are thin compatibility wrappers around the utf8 module. For new code, prefer using utf8.RuneCountInString, utf8.Valid, and utf8.EastAsianWidth directly. - LEEncodeInt32(n int) (string, error) / LEDecodeInt32(b string) (int, error) - LEEncodeInt64(n int) (string, error) / LEDecodeInt64(b string) (int, error) - LEEncodeFloat32(f float64) (string, error) / LEDecodeFloat32(b string) (float64, error) - LEEncodeFloat64(f float64) (string, error) / LEDecodeFloat64(b string) (float64, error) - BEEncodeInt32 / BEDecodeInt32 - BEEncodeInt64 / BEDecodeInt64 - BEEncodeFloat32 / BEDecodeFloat32 - BEEncodeFloat64 / BEDecodeFloat64 - LEB128UEncode(n int) (string, error) / LEB128UDecode(b string) (int, error) - LEB128SEncode(n int) (string, error) / LEB128SDecode(b string) (int, error) - Base58Encode(s string) (string, error) / Base58Decode(s string) (string, error) - TxtarParse(text string) (map, error) / TxtarWrite(archive map) (string, error) - Iconv(s string, from string, to string) (string, error) - IconvEx(s string, from string, to string, opts map) (string, error)

XML Node Shapes

  • Basic:
  • name: string
  • attrs: map[string]string
  • text: string
  • children: array of nodes

  • Mixed-content:

  • name: string
  • attrs: map[string]string
  • children: array of (string | node)

  • Namespace-aware:

  • name: string
  • space: string
  • attrs_ns: array of {name, space, value}
  • text: string
  • children: array of nodes

Examples

Base64 / Base64URL

Base64 and Base64URL
1
2
3
4
5
6
7
8
import encoding

var raw = "hello, world\n"
var b64, _ = encoding.Base64Encode(raw)
var back, _ = encoding.Base64Decode(b64)

var urlB64, _ = encoding.Base64URLEncode(raw)
var urlBack, _ = encoding.Base64URLDecode(urlB64)

Base32

Base32
1
2
3
4
import encoding

var b32, _ = encoding.Base32Encode("data")
var b32Back, _ = encoding.Base32Decode(b32)

Base58 (Bitcoin alphabet)

Base58
1
2
3
4
import encoding

var b58, _ = encoding.Base58Encode("bytes")
var b58Back, _ = encoding.Base58Decode(b58)

Hex

Hex
1
2
3
4
import encoding

var hx, _ = encoding.HexEncode("\x00\x01ABC")
var hxBack, _ = encoding.HexDecode(hx)

CSV

CSV Read/Write/Parse
import encoding

// ReadAll
var text = "name,age\nAlice,30\nBob,25\n"
var rows, _ = encoding.CSVReadAll(text)

// WriteAll
var csvOut, _ = encoding.CSVWriteAll(rows)

// Parse with options
var opts = {"comma": ";", "trim_space": true}
var parsed, _ = encoding.CSVParse("a;b;c\n1;2;3\n", opts)

HTML escape/unescape

HTML Escape/Unescape
1
2
3
4
import encoding

var esc, _ = encoding.HTMLEscape("Tom & Jerry <3")
var unesc, _ = encoding.HTMLUnescape(esc)

XML escape/unescape

XML Escape/Unescape
1
2
3
4
import encoding

var xesc, _ = encoding.XMLEscape("5 < 7 & 9 > 2")
var xunesc, _ = encoding.XMLUnescape(xesc)

XML basic marshal/unmarshal

XML Marshal/Unmarshal
1
2
3
4
5
import encoding

var xml = "<person id=\"42\"><name>Alice</name><age>30</age></person>"
var node, _ = encoding.XMLUnmarshal(xml)
var out, _ = encoding.XMLMarshal(node, true)

XML mixed-content

XML Mixed Content
1
2
3
4
5
import encoding

var xml = "<p>Hello <b>world</b> &amp; universe!</p>"
var node, _ = encoding.XMLUnmarshalMixed(xml)
var out, _ = encoding.XMLMarshalMixed(node, true)

XML namespace-aware

XML Namespace-aware
1
2
3
4
5
import encoding

var xml = "<x:note xmlns:x=\"urn:ex\"><x:to>Alice</x:to></x:note>"
var node, _ = encoding.XMLUnmarshalNS(xml)
var out, _ = encoding.XMLMarshalNS(node, true)

UTF-8 helpers (compatibility)

UTF-8 Helpers
1
2
3
4
5
6
7
import encoding

var s = "こんにちは"
var count, _ = encoding.UTF8RuneCount(s)      // rune count
var valid, _ = encoding.UTF8Valid(s)          // true/false
var width, _ = encoding.UTF8EastAsianWidth(s) // simple width metric
// Note: For new code, prefer utf8.RuneCountInString / utf8.Valid / utf8.EastAsianWidth.

Little-endian binary helpers

Little-endian Helpers
import encoding

var i32b, _ = encoding.LEEncodeInt32(123456)
var i32, _ = encoding.LEDecodeInt32(i32b)

var i64b, _ = encoding.LEEncodeInt64(987654321)
var i64, _ = encoding.LEDecodeInt64(i64b)

var f32b, _ = encoding.LEEncodeFloat32(3.14)
var f32, _ = encoding.LEDecodeFloat32(f32b)

var f64b, _ = encoding.LEEncodeFloat64(6.28)
var f64, _ = encoding.LEDecodeFloat64(f64b)

Big-endian binary helpers

Big-endian Helpers
import encoding

var bi32b, _ = encoding.BEEncodeInt32(123456)
var bi32, _ = encoding.BEDecodeInt32(bi32b)

var bi64b, _ = encoding.BEEncodeInt64(987654321)
var bi64, _ = encoding.BEDecodeInt64(bi64b)

var bf32b, _ = encoding.BEEncodeFloat32(3.14)
var bf32, _ = encoding.BEDecodeFloat32(bf32b)

var bf64b, _ = encoding.BEEncodeFloat64(6.28)
var bf64, _ = encoding.BEDecodeFloat64(bf64b)

LEB128 (unsigned and signed)

LEB128
1
2
3
4
5
6
7
import encoding

var uleb, _ = encoding.LEB128UEncode(624485)
var uval, _ = encoding.LEB128UDecode(uleb)

var sleb, _ = encoding.LEB128SEncode(-12345)
var sval, _ = encoding.LEB128SDecode(sleb)

txtar parse/write

txtar Parse/Write
import encoding

var txt = """
-- hello.txt --
Hello
-- data.bin --
bytes
"""
var arch, _ = encoding.TxtarParse(txt)
var round, _ = encoding.TxtarWrite(arch)

Iconv and IconvEx

Iconv and IconvEx
1
2
3
4
5
6
7
8
import encoding

var s = "Olá — mundo"
var cp, _ = encoding.Iconv(s, "utf-8", "windows-1252")
var back, _ = encoding.Iconv(cp, "windows-1252", "utf-8")

var opts = {"replace_unsupported": true, "normalize_aliases": true}
var cp2, _ = encoding.IconvEx(s, "utf8", "cp1252", opts)

Notes

  • Iconv/IconvEx use golang.org/x/text encodings; ensure dependency is added to go.mod.
  • Binary helpers treat strings as byte buffers. Use with care if you need printable data (e.g., wrap with Base64).