Hidden Go Tricks: Numeric Literals, Slice Expressions, Test Caching, and JSON Escape Control

This article reveals lesser‑known Go language details, covering numeric literal formats, slice expression variations, test result caching with go test, and how to disable HTML escaping during JSON serialization, complete with code examples and output.

Go Development Architecture Practice
Go Development Architecture Practice
Go Development Architecture Practice
Hidden Go Tricks: Numeric Literals, Slice Expressions, Test Caching, and JSON Escape Control

Go has become a dominant language for modern backend systems due to its simplicity, efficiency, and strong concurrency support. This guide shares several practical tips that many developers may overlook.

01. Numeric Literals

Go allows binary ( 0b...), octal ( 0o...), hexadecimal floating‑point ( 0x1p-2) and decimal floating‑point literals. Underscores can be used as visual separators:

v1 := 0b00101101   // binary
v2 := 0o377        // octal
v3 := 0x1p-2       // hex float
v4 := 01e2         // decimal float
v5 := 123_456      // underscore separator

02. Slice Expressions

Slice expressions create sub‑slices from arrays, slices, strings, or pointers to arrays. The basic form a[low:high] includes low and excludes high. Examples:

a := [5]int{1, 2, 3, 4, 5}
s := a[1:3]               // s = [2 3], len=2, cap=4
b := "hello world"
s2 := b[1:3]              // s2 = "el", len=2

Omitted indices default to the start or end of the operand ( a[2:], a[:3], a[:]). Index bounds must satisfy 0 ≤ low ≤ high ≤ len(a); otherwise a runtime panic occurs. When slicing a slice, the upper bound is the slice’s capacity, not its length.

A full slice expression a[low:high:max] also sets the resulting slice’s capacity to max‑low. The first index may be omitted (defaults to 0). Example:

a := [5]int{1, 2, 3, 4, 5}
s1 := a[1:3:4]   // len=2, cap=3
s2 := a[1:3]      // len=2, cap=4

Full slice expressions require 0 ≤ low ≤ high ≤ max ≤ cap(a). Note that strings do not support the three‑index form.

03. go test Cache

When running go test in package list mode, successful test results are cached. Subsequent runs with identical cache‑eligible flags ( -benchtime, -cpu, -list, -parallel, -run, -short, -timeout, -failfast, -v) will retrieve the cached output, shown as (cached) in the summary line.

❯ go test . -v
=== RUN   TestSplit
--- PASS: TestSplit (0.00s)
PASS
ok   split 0.005s
❯ go test . -v
=== RUN   TestSplit
--- PASS: TestSplit (0.00s)
PASS
ok   split (cached)

Adding any non‑cacheable flag disables caching. To force a fresh run, use -count=1:

❯ go test . -v -count=1
=== RUN   TestSplit
--- PASS: TestSplit (0.00s)
PASS
ok   split 0.005s

04. JSON Serialization Without HTML Escaping

The encoding/json encoder escapes &, <, and > by default to avoid XSS issues. Call Encoder.SetEscapeHTML(false) to disable this behavior when HTML safety is not required.

// Default escaping
b, _ := json.Marshal(data)
fmt.Printf("json.Marshal(data) result:%s
", b)

// Disable escaping
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
enc.Encode(data)
fmt.Printf("encoder.Encode(data) result:%s
", buf.String())

Running the example with a URL containing an ampersand produces:

json.Marshal(data) result:{"URL":"https://liwenzhou.com?name=q1mi\u0026age=18"}
encoder.Encode(data) result:{"URL":"https://liwenzhou.com?name=q1mi&age=18"}

These tips help Go developers write clearer, more efficient code and better understand the language’s nuances.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

go test cachejson escapehtmlnumeric literalsslice expression
Go Development Architecture Practice
Written by

Go Development Architecture Practice

Daily sharing of Golang-related technical articles, practical resources, language news, tutorials, real-world projects, and more. Looking forward to growing together. Let's go!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.