Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Building a Go Command Line Tool

Go is a great language for building tools. In this learning activity, you'll go through the process of building a complete command line application. By the time we've finished this activity, you'll have gone from a list of features and a desired user experience to having a fully built CLI written in Go. You'll learn how to create custom structs and work with JSON along the way.

Google Cloud Platform icon
Lab platform
Lab Info
Level
Intermediate
Last updated
Sep 16, 2025
Duration
2h 0m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Create `hr` Package within the `$GOPATH`

    Our project needs to be created. We'll do so by creating a directory for us to add our source files.

  2. Challenge

    Create `main` Package with `User` Struct to Hold Information Parsed from `/etc/passwd`

    Our list of users has a very specific shape, and we can represent that shape using a struct. Let's create a main.go file and add a User struct to it that handles the name, id, home, and shell values.

  3. Challenge

    Parse Information from `/etc/passwd` into a Slice of `User` Structs

    We're going to create a function called collectUsers that parses the /etc/passwd file and returns a slice of User structs. Now we'll be able to use the user information in the main portion of our program.

  4. Challenge

    Define and Parse the `path` and `format` Flags

    To keep things manageable, we're going to define our flags in a separate function that performs the following steps:

    1. Defines the path and format flags
    2. Parses flags
    3. Validates the user-provided information. If there is an error, then exits and displays an error.
    4. Returns path and format values
  5. Challenge

    Write User Information as CSV or JSON Information Either to a File or stdout

    We want to write the content to either a file or stdout, and thankfully they both adhere to the io.Writer interface. Because of this interface, we can keep our writing logic ignorant of whether we're writing to a file or stdout. Here's what we need to implement, in order:

    1. Parse flags into variables using parseFlags.
    2. Gather users using collectUsers.
    3. Create an io.Writer variable.
    4. Assign a value to the io.Writer variable based on the path variable. This variable should be a file if the path variable is set or os.Stdout otherwise.
    5. If the format variable is "json", utilize the json.MarshalIntent function to pretty-print the users to a slice of bytes before using the Write function on our io.Writer variable. OR
    6. If the format variable is "csv", write the header line to the io.Writer, create a new csv.Writer from the io.Writer, and write a line for each of the users.
About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight