Go Examples

Page 2 of 8

Browse Go examples

goSnippet

Relative Paths With filepath.Rel

Turn absolute targets into root-relative paths with filepath.Rel for reports and allowlist checks, expecting the leading .. when a target escapes the root.

goSnippet

First Unique Rune Via Map

Count runes with a map, then rescan for the first rune whose count is one, returning its byte index so the winner can be sliced out of the string.

goSnippet

Kadane Max Subarray Sum

Sweep a slice once with Kadane: reset the running sum when it turns negative, track the best window, and print both the sum and the winning subarray.

goSnippet

Matrix Transpose With Slices

Rebuild a matrix with rows and columns exchanged through nested loops over a fresh [][]int, keeping the source untouched for shape-aware transformations.

goSnippet

Send On Closed Channel Panics

Receiving from a closed channel stays safe and drains buffered values before zero-and-false, while a send panics until recover catches it.

goSnippet

Sentinel Value Stops Workers

A reserved sentinel job value makes a worker exit early, and closing the jobs channel stops the remaining workers waiting on receive.

goSnippet

Single Number Found By Xor

Cancel out duplicate values with a single xor accumulator: identical pairs annihilate, leaving the one number that appears exactly once in the slice.