Skip to main content
bash TV

What Is Hiding on Your Disk? Go Cobra `scan` Command

Flo Woelki

642 views16 Oct 2026

YouTube

Implementing our simple scan command with Cobra and Go is much easier than you might think! You can check out the first part right here: https://youtu.be/7q7cyK4-jmM 📝 Description Add your first proper subcommand to a Go CLI built with Cobra: scan. In this second part of the mini-series, we build a command that walks a directory and displays file sizes, with an optional flag to mark large files. That makes it easy to find what's eating your disk space. Here's what you will learn: • Create a Cobra subcommand with `Use`, `Short`, `Long`, and aliases, and register it with `AddCommand` inside init. • Define local flags such as `--path` and `--mark` with shorthands, and see how they differ from persistent flags like `--verbose`. • Validate input up front and return wrapped errors with `%w` using `RunE`. • Walk a directory recursively with `filepath.WalkDir`, handle errors without stopping the scan, and convert bytes to MB without integer division bugs. ⏳ Timestamps: 00:00 - Introduction 01:14 - scan implementation 18:53 - Testing 21:30 - Outro 🔗 Links: cobra: https://github.com/spf13/cobra 👋 Hey there! If you are new to this channel: Hey 👋 my name is Flo and I am a professional software engineer with a passion for coding in Golang, TypeScript, JavaScript, and Rust. This channel is where I share that passion and dive into the exciting world of software engineering. 📨 Support and Connect! Become a member. https://www.youtube.com/@FloWoelki/membership Discord: https://discord.gg/TnA7drJ X / Twitter: https://x.com/FlorianWoelki LinkedIn: https://www.linkedin.com/in/florian-woelki/ GitHub: https://github.com/FlorianWoelki 📝 Copyright Notice The original Go Gopher was designed by Renée French (Source: https://go.dev/blog/gopher License details: https://creativecommons.org/licenses/by/4.0/). The gopher appearing in this video and thumbnail has been modified from the original design and is based on an illustration from Maria Letta's free-gophers-pack (Source: https://github.com/MariaLetta/free-gophers-pack License: CC0 1.0 Public Domain https://creativecommons.org/publicdomain/zero/1.0/). #go #cobra #cli #scan

Join the discussion

Sign in to join the discussion

Sign in