Join Miki Tebeka for an in-depth discussion in this video Solution: Key-value database, part of Go Essential Training.
- [Instructor] We first define a database…which is a map from string to the empty interface…and we initialize it with an empty map.…This the second curly braces.…And then we create a lock…which is Mutex which will give exclusive read…or write to the database.…We define an Entry struct.…This will match both the POST and the GET request.…It has a key in the database…and the value which is an empty interface…meaning it can be a string or an integer or a float.…
Since both the POST and the GET…are sending back an entry as JSON,…I wrote a utility function…called sendResponse that gets an entry and ReponseWriter.…It sets the content type of the response…to application/json,…create a JSON encoder…and then encodes the entry.…If there is an error, it will log the error.…The PostHandler handles a request for setting…or altering a value in the database.…We make sure that the body is closed with a defer…and then we create a new JSON decoder…and decode the request to the entry.…
If there is an error,…it will send an http.Error and return.…
- Go basic types, conditionals, and loops
- Go functions
- Object-oriented programming with Go
- Error handling
- Concurrent programming with channels
- Testing and project management
- Networking with JSON and HTTP
Skill Level Beginner
Programming Foundations: Fuzzy Logicwith Erin Colvin41m 50s Intermediate
Getting started1m 34s
1. Getting Started
2. Go Basics
5. Error Handling
7. Project Management
Next steps1m 12s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.