Introduction
In episode 8, Miki developed a Go HTTP client that had a method to check the health of a theoretical API. The method would construct the request URL and return an error based on the response code received from the server. Miki then created a type that would mock Go’s http.RoundTripper
interface and replace his client’s transport with said type to test it. To improve the efficacy, Miki’s stub will simulate an erroneous response to see if his client’s Health
method is working as intended.
In this video, Miki will write a program that decodes a command line flag representing an address into a Go struct. Prior to writing any code, Miki lays out the approach he’ll be taking and how the flag
package leverages the Value
interface to decode flags passed from the command line. The next step Miki takes is to create a struct type that implements the flag.Value
interface. One of this implementation’s highlights is how Miki makes use of the Set
method to perform validation on the string passed to ensure the address is formatted properly. Watch and learn how you can decode a command flag into a user-defined struct type.
Things you will learn in this video
- How to decode a command flag into a Go type.
- The role of each method found on the
flag.Value
interface. - How to validate a user-defined flag value.
Video