From the course: Transition from Java to Go

Unlock this course with a free trial

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

Handling JSON

Handling JSON

- [Instructor] Handling JSON input from external sources is a common task we do as software engineers. Let's have a look at how this works in Go. Looking back at the temperature service, we see it creates cities and prints them out one by one. This is repetitive and turns. It's time to refactor this code using all we have learned about data structures. We will take on three refactors. We will read input data from a JSON file saving us from declaring this information and code. Then, we will save the cities into a data structure instead of declaring each entry in its own local variable. Once saved in a data structure, we will use the range operator to print out the saved data instead of calling the print method for each variable. This refactor will clean up the main file and make it a more generic service. The JSON format is a versatile and widely used. The Go standard library provides built in support for JSON encoding and…

Contents