Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Go Programming Bootcamp

Lesson 40 of 60 · Go

Building a Simple CLI Tool – Argument Parsing

Duration: 15 minutes

Argument Parsing with flag

This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.

import "flag"

func main() {
    name := flag.String("name", "world", "name to greet")
    repeat := flag.Int("n", 1, "how many times to greet")
    flag.Parse()
    for i := 0; i < *repeat; i++ {
        fmt.Printf("Hello, %s!\n", *name)
    }
}

Run:

$ go run greet.go -name=Bob -n=3
Hello, Bob!
Hello, Bob!
Hello, Bob!

Sub‑commands with Cobra (see lesson‑23)

Info

Prefer the standard flag package for tiny tools; use Cobra for complex CLIs.

Previous: Testing Concurrency – Race Detector and `testing`Next: Logging & Structured Tracing with OpenTelemetry