Lab 6: GoSpring 2022

This lab seeks to help introduce you to Go, the programming language used in Project 3. It uses the Tour of Go—a great resource developed by the creators of the language—and also features some choice guidance related to the specifics of the project.

Setup

To get the code for this lab, create a repo using the GitHub template. Make sure to make this repo private. Clone the repo onto your system, then open it in VS Code, reopening the folder in the development container as in other projects.

Tasks

A Tour of Go

A Tour of Go is a fantastic resource developed and maintained by the creators of Go and its wider community which features runnable, interactive examples narrating a walkthrough of the basics of the language, challenging you to solve problems as you go. While it might seem like laziness on our part to link you to this, it would be a disservice not to—these examples have been curated over the course of many years with millions of eyeballs meticulously examining and improving them. We’d be hard-pressed to come up with a better introduction on our own.

Complete the “Using the tour”, “Basics”, and “Methods and interfaces” sections of the tour. (You do not need to complete the “Generics” or “Concurrency” sections, but are free to if you are interested!) For each page labeled “Exercise”, include your solution in the corresponding file in the tour-exercises directory in the lab starter code.

Because the Tour has been so extensively studied and developed, plenty of information is available on the internet if you get stuck. Feel free to reference these—getting stuck on intricacies of a new language is never fun, nor is it typically productive. Please do, however, first put some effort into solving the exercises on your own—every exercise details a key aspect of Go, and simply copying existing solutions will likely not work out very well in the long run on Project 3. Although working through the exercises will take some time, truly understanding them will pay dividends in time savings once you begin working on the project. (Trust us: we speak from experience.)

Reader Semantics

A common source of confusion for newcomers to Go is the semantics around the io.Reader type. A Reader is a “stream” of bytes, meaning that it provides a source of data to read from, however this is not inherently a data structure that stores bytes. It is more of a “bucket” that can output data—reading drains the bucket, and this is a one-way operation. Think of the data in a TCP connection—it can be read, but it would be extremely wasteful to store all data from a connection in a single data structure! This can cause issues, however, when operations unexpectedly read from a reader, causing subsequent reads to return no more data.

You are tasked with implementing the echo HTTP handler in lab6.go such that it echos back the request body to the response body if and only if the command parameter has the value echo. Look into the documentation on http.Request for more information on retrieving parameters from the request form. Because the parameters are in the body, this requires the body to be read; this may cause issues in echoing back the body! This may be a bit tricky. Do some research into the topic; it will likely serve you in further work in Project 3!

You can test your solution via the test functions in lab6_test.go. Run these via go test . on the command line or by hitting “run test” or “debug test” on the individual test functions in the file in VS Code. (This is a great chance to get some experience with the Go debugger!)

Submission

Submit the various files to the Autograder by the deadline.