---
title: "goutil: Substr panic on a negative length in Go | VulCode"
description: "In goutil we fixed strutil.Substr, which panicked in Go when a negative length trimmed more than the rest of the string. It now returns an empty string."
url: "https://vulcode.com/en/opensource/contributions/goutil"
lang: "en"
---

[All contributions](https://vulcode.com/en/opensource/contributions)

gookit/goutil

# goutil

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

[See the repository](https://github.com/gookit/goutil) [All contributions](https://vulcode.com/en/opensource/contributions)

- **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](https://github.com/gookit/goutil/pull/265)

## Merged change 1

- [#265 fix(strutil): prevent Substr panic on out-of-range negative length Merged 7 September 2026](https://github.com/gookit/goutil/pull/265)

## 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](https://vulcode.com/en/technologies/go)

## Other projects we write code for

- [**urfave/cli** Go · 24,329 stars A declarative Go package for building command line tools.](https://vulcode.com/en/opensource/contributions/urfave-cli)

- [**chezmoi** Go · 22,342 stars A dotfiles manager that keeps the same settings on many different machines, securely.](https://vulcode.com/en/opensource/contributions/chezmoi)

- [**goquery** Go · 14,995 stars A Go library for traversing and changing HTML documents in the style of jQuery, built on the golang.org/x/net/html parser.](https://vulcode.com/en/opensource/contributions/goquery)

- [**Ebitengine** Go · 13,647 stars A 2D game engine in Go, used in games released for computers, phones, browsers and consoles.](https://vulcode.com/en/opensource/contributions/ebiten)

- [**Fission** Go · 8,950 stars A serverless functions framework for Kubernetes that runs code as functions and scales them down to zero when they are idle.](https://vulcode.com/en/opensource/contributions/fission)

- [**Concourse** Go · 7,944 stars A container-based automation system written in Go and used mostly for CI/CD. Pipelines and tasks are described in YAML files.](https://vulcode.com/en/opensource/contributions/concourse)

## Describe your project.

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

### Rather skip the form?

Ask in the chat or write to us directly.

Open the chat

- **Email**: [hello@vulcode.com](mailto:hello@vulcode.com)
- **Phone**: [+48 576 043 920](tel:+48576043920)
- **Discord**: [discord.gg/vulcode](https://discord.gg/vulcode)
