All contributions

gookit/goutil

goutil

A collection of over 900 helper functions for Go, covering strings, numbers, maps, structs, files, time and testing.

Language
Go
GitHub stars
2,361
Merged changes
1

What we changed

strutil.Substr no longer panics on an out-of-range negative length and returns an empty string instead.

What this work shows

  • Slices and runes in Go
  • Index range edge cases
  • Unit tests in Go
What was broken

Substr panicked on a negative length

A negative length in strutil.Substr means how many characters to cut from the end of the string. When the cut reached past the start position, for example hello with position 4 and length -3, the slice runes[4:2] panicked with slice bounds out of range.

How we fixed it

The slice end never goes before the start

The stop index is clamped to the start position, so an over-long cut gives an empty string, just like a position beyond the text. TestSubstr gained the three cases that used to panic.

#265

Merged change 1

  1. #265 fix(strutil): prevent Substr panic on out-of-range negative length Merged 7 September 2026

What it taught us

A helper function is worth testing at the edges of its contract, because that is where the input that makes Go panic lives. We turned the panic into a predictable result consistent with the rest of the function.

More about this technology: Golang

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.