From the course: Learning Go (2021)

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Solution: Read a shopping cart from JSON

Solution: Read a shopping cart from JSON - Go Tutorial

From the course: Learning Go (2021)

Solution: Read a shopping cart from JSON

(bright music) - [Instructor] In this code challenge you're asked to take a string which represents a json data set. In this example, it's a shopping cart with three instances of a struct named cart item. Now, each of these objects has a name, which is a string, a price, which is a float and a quantity, an integer. And when I pass that string into my function, getCartFromjson, I get back a slice of cart item objects. I'll test my code and show that I got back a correct response. And here's how I did it. First of all, at the top of my code, I added an import statement to include the package encoding/json. Next, within the getCartFromjson function I called the json object unmarshal function. I pass in a byte slice, which is wrapped around the json string and I return that value to the cart object. If there are any errors I panic and display the error message. Now, there's a little wrinkle here. First of all, I'm…

Contents