These are the code-samples, exercises, quizzes, and links needed for this course. To complete these exercises follow the instructions in this doc. This section has one folder for each exercise/solutions. Your solutions do not need to match the provided solutions. The goal of these exercises is to learn and understand the why things are implemented, so that a participant can apply principles to any framework or problem set.
Exercise 1 Build a Standard Library Server
In a new main.go
file, create a web server using the ListenAndServe()
function. This server should have at least one endpoint that accepts a parameter and returns it in a response. An example of a finished exercise in in ex-1-server
- reference: go-by-example Https server
NOTE: ListenAndServe()
is great for prototyping and non-production development. When you are building out your services, make sure you have security in mind. Use a custom mux
. We will learn more about security best practices later on in the course. Check out Reliable web services.
Exercise 2 Build a Server Using an open source Framework
The Standard Library provides all the functionality needed to build a robust web server, but sometimes, instead of building out your own infrastructure, there is a benefit to adopting an open source frameworks at the backbone of your server infrastructure. This exercise is you chance to experiment with some popular Go web frameworks.
Instructions: Pick a web framework and implement a server that can accept a username, validate it, and print the username to standard out. Add logic to handle duplicate usernames, empty username parameters, or any additional error cases. When a username is accepted and printed to standard out return a 200 class status code. If the parameter is empty or invalid return a 400 class error code. Handle any other errors. We will connect this server to a database in the Day 2 exercises
Examples of the finished exercise:
- using the Chi web framework.
- using the Fiber web framework.
Note: you do not have to do this with a web framework. Feel free to use the standard library net/http
tooling if you are more comfortable. The options for using a web framework are just to try you hand at evaluating popular open source tools. But just like adding any tool to your software start there are risks and rewards.
Here are some examples of open source frameworks:
Exercise 3 Build a Go app that calls your new endpoint.
Using the standard library to create a client that calls your new web endpoint. To connect to you server, you will need to run your server in one different and the client app in a different terminal window.
Extra Practice: Lots of companies use Postman to test their service endpoints. Build a client in postman that will call your server. Save you postman solution and generate a go client from the Postman UI. Does you generated go code match the one you build?
Using the httptest package add some tests endpoints in [ex-4-test/server.go]. You can use the file server_test.go to implement the pass and fail checks for each of the endpoints. You should have one test that checks for a 200s class error and one that tests for a 400s class error.
Extra Practice: If you prefer one of the frameworks we used in the example try writing tests for it. Using the endpoints you wrote in Exercise 3
write tests to simulate a 200 class status code, a 400 class status code, and a 500 class status code.
Here are some examples of tests for the above open source frameworks.
We will be using curl
to test the functionality of our endpoints and servers. If you are unfamiliar with the curl
syntax check out these resources: