/goworkshop

Go workshop repository

Primary LanguageGoMIT LicenseMIT

golang workshop

Part 1 - Write REST services with data loaded from files

1. hello world in GO

  1. create main directory
  2. create main.go file
  3. declare package main
  4. declare main function
  5. import fmt package
  6. make a call to fmt.Println and print "Hello world, my name is <your_name_here>"

2. defining the data model

  1. create datamodel.go file
  2. create AuthorDto struct with the following fields:
  • UUID string
  • FirstName string
  • LastName string
  • Birthday string
  • Death string
  1. create BookDto struct with the following fields:
  • UUID string
  • Title string
  • NoPages int
  • ReleaseDate string
  • Author AuthorDto
  1. create a slice of AuthorDto which will hold all authors in the system
  2. create a slice of BookDto which will hold all books in the system

3. add JSON marshalling

AuthorDto JSON mappings:

  • map AuthorDto.UUID field to uuid field in the resulting JSON
  • map AuthorDto.FirstName field to firstName field in the resulting JSON
  • map AuthorDto.LastName field to lastName field in the resulting JSON
  • map AuthorDto.Birthday field to birthday field in the resulting JSON
  • map AuthorDto.Death field to death field in the resulting JSON

BookDto JSON mappings

  • map BookDto.UUID field to uuid field in the resulting JSON
  • map BookDto.Title field to title field in the resulting JSON
  • map BookDto.NoPages field to noPages field in the resulting JSON
  • map BookDto.ReleaseDate field to releaseDate field in the resulting JSON
  • map BookDto.Author field to author field in the resulting JSON

4. read sample data from files

  1. create model package
  2. move datamodel.go in model folder
  3. create importer package
  4. create authors.json file in importer folder
  5. add an array of authors with sample data in JSON format
  6. create books.json file in importer folder
  7. add an array of books with sample data in JSON format
  8. create dataImporter.go file in importer folder
  9. import encoding/json package
  10. import io/ioutil
  11. create a function named ImportAuthors that reads authors.json file and return a slice of authors
  12. create a function named ImportBooks that reads the books.json file and returns a slice of books
  13. print all authors to the STDOUT
  14. print all books to the STDOUT

5. start a web server, listening on a configured port

  1. create web package
  2. create webserver.go
  3. import net/http
  4. read listening port from API_PORT environment variable, defaulting to 8000
  5. define a http.Handler function that will handle the incoming HTTP requests to the web server
  6. launch the web server on API_PORT port by calling ListenAndServe
  7. if the web server returns an error, exit the application

6. writing a simple REST endpoint

  1. change the path of the REST endpoint from /test to /books
  2. configure the handler to respond only to GET methods
  3. marshal the books array to JSON bytes
  4. convert the JSON bytes to string
  5. set the "Content-Type" response header to "application/json"
  6. when /books endpoint is hit, return all books previously loaded from the JSON file

7. implement all REST endpoints with data loaded from files

Part 2 - Add unit tests for all REST endpoints and create the persistence layer

8. write unit tests for all REST endpoints

9. add persistence mappings

10. add persistence services to retrieve data from db

11. switch from loading data from db instead of files

#Part 3 - Add error handling and logging, improve performance with Go routines

12. add error handling

13. add logging support

14. create a configuration service for the application

15. improve performance by handling requests async using Go routines