Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
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
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 2h 0m
Published
Clock icon Jan 21, 2019

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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans