Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Member-only story

Unveiling Go’s Hidden Gems: Features You Might Have Missed

--

Fuzzing is one of the powerful automated testing techniques which aims to find out the bugs and vulnerabilities in your code by feeding it a vast array of random or semi-random inputs.

Press enter or click to view image in full size
Image generated by C program

Go’s built-in fuzzing support makes it remarkably easy to integrate this valuable practice into your development workflow.

func ParseNumber(s string) (int, error) {
// ... (your parsing logic here)
}

func FuzzParseNumber(f *testing.F) {
f.Add("123", 456) // Seed corpus with some valid inputs
f.Fuzz(func(t *testing.T, a, b string) {
_, errA := ParseNumber(a)
_, errB := ParseNumber(b)

if (errA == nil) != (errB == nil) {
t.Errorf("Inconsistent error for '%s' and '%s'", a, b)
}
})
}

In this example, FuzzParseNumber is a fuzz test that will automatically generate a variety of strings (a and b) and check if your ParseNumber function handles them consistently (i.e., either both succeed or both fail).

Let’s see an another example

package main

import (
"fmt"
"testing"
)

func ReverseString(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}

func FuzzReverseString(f…

--

--

Kirubakaran
Kirubakaran

Written by Kirubakaran

Software Engineer expertise on C, C++, Golang, Python, Docker, Kubernetes.

Stackademic
Stackademic

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.