Introduction
In the previous chapter, we discussed error handling in Go. We looked at what an error is in Go; it is anything that implements the error interface. At the time, we did not investigate what an interface was. In this chapter, we are going to look at what an interface is.
For example, your manager requests that you create an API that can accept JSON data. The data contains information about various employees, such as their address and the hours they worked on a project. The data will need to be parsed into an employee
struct, a relatively simple task. You then create a function called loadEmployee(s string)
. The function will accept a string that is formatted as JSON, and then parse that string to load the employee
struct.
Your manager is happy with the work; however, they have another requirement. The clients need the ability to accept a file with the employee data in JSON format. The functionality to be performed is the same underlying task as before. You create...