From nil to Gopher

From nil to Gopher

This presentation is about my journey while I was learning GO and building IMGART project.

1bbe893cbb494c230fe608b3783ac7bd?s=128

Marco Talento

October 24, 2018
Tweet

Transcript

  1. From nil to Gopher GopherLX Marco Talento 24/10/2018

  2. WHY GO?

  3. https://pbs.twimg.com/media/B79paXxIcAAu0CN.jpg

  4. Why GO? • Compiled static language • Simple (25 keywords)

    and performant • GO routines and concurrency model • Great Tooling (testing, benchmarking, profiling) • Cross compilation and easy to deploy (single binary) • Awesome community (gophers.slack.com)
  5. Learning GO https://bit.ly/2pOQT8J

  6. Learning GO • I started Learning GO in 2017 •

    A Tour of GO (https://tour.golang.org) • Books (The GO programming Language) • Videos and Courses: JustForFunc, GopherCon, Ardan Labs, Todd Macleod • Looking at Open Source Projects (Upsin, Apex, InfluxDB, HashiCorp, etc...)
  7. Let's build something https://golang.org/doc/gopher/pencil/

  8. Requirements • Building something useful • Try to use most

    of the go features • Good DX (Developer experience) • Testing and Quality
  9. IMGART

  10. IMGART • HTTP service for image manipulation • Configurable profiles

    • Image Caching • Source Code: https://github.com/Talento90/imgart
  11. Example GET /api/v1/images?imgSrc=https://bit.ly/2JFW2HO &filters=[{"id":"overlay","parameters":{"position":[25,75],"url":"https://goo.gl/UBrXeo"}},{"i d":"overlay","parameters":{"position":[22,-35],"url":"https://goo.gl/aEkkDh"}}, {"id":"crop","parameters":{"rectangle":[0,0,202,150]}}] &profile=my-custom-profile ++ + +

    =
  12. Where do I start?

  13. Question and Decisions • How should I organize my application?

    • Error Handling? • How to Debug? • How to maintain code quality? • Should I care about cancellation?
  14. How to structure GO applications?

  15. Structure Application • Flat packages (everything in the root level)

    • Organize packages by components (models, controllers, repositories) • Clean Architecture (Entities, Use Cases, Adapters) • Hexagonal Architecture (Domain, Application, Infrastructure) • How do you structure your GO apps - Kat Zien talk at GopherCon 2018
  16. Structure Application • Group packages by domain • Dependencies Inwards

    • No circular dependencies • Isolate external dependencies
  17. Code Structure • /root folder - configuration, Dockerfiles, CI/CD ◦

    makefile - scripts for debug, run tests, build • /cmd - Main applications (api/main.go, cli/main.go) • /mock - Mocks for external dependencies • /httpapi - HTTP service • /imgart - Core Domain (entities, interfaces) • /effect, image, profile - Domain Logic • /repository - Repository implementations (redis, mongo)
  18. Error Handling https://bit.ly/2d0qz5C

  19. Errors • Error is a bug in a program that

    causes unexpected behaviour • Good errors must give helpful information for programmers • Errors should be divided: ◦ Application Error e.g. Error establishing a database connection ◦ User Error e.g. An internal error has occurred. Please contact support
  20. Errors in GO https://golang.org/src/errors/errors.go

  21. Application Errors • Errors should have an internal type (e.g.

    permissions, conflict) • Readable message with a clear context (e.g. Profile id: 123 does not exist) • The original error (Debug purposes)
  22. Example: Handle Errors to Status Codes

  23. Debugging with package fmt fmt.Printf("debug: %s", value)

  24. None
  25. None
  26. Delve • Debugger for GO • Installation using go get

    ◦ go get -u github.com/derekparker/delve/cmd/dlv • Run dlv command ◦ dlv debug github.com/talento90/imgart/cmd/imgartapi ◦ dlv test github.com/talento90/imgart/image • Open Source: https://github.com/derekparker/delve
  27. Testing and Quality Tools

  28. Testing Package • Package testing provides support for automated testing

    of Go packages ◦ go test -v -race ./... • Package that provides support for HTTP testing: net/http/httptest • Find code using the following pattern: ◦ func TestXxx(*testing.T) • Supports Benchmarking tests ◦ func BenchmarkXxx(*testing.B)
  29. Table testing https://github.com/golang/go/wiki/TableDrivenTests

  30. Quality Tools • go vet - Examines code and reports

    suspicious constructs • golint - Linter for Go source code • gocyclo - Calculates cyclomatic complexities • megacheck - Static Code Analyser • More tools: https://staticcheck.io, https://goreportcard.com
  31. Why cancellation matters?

  32. Request Lifecycle Request Lifecycle Download Image Input Process Image Response

    User Cancel Request
  33. context.Context https://golang.org/pkg/context/

  34. context.Context Errors

  35. ctx.Context Example

  36. HTTP Requests (Download Image)

  37. Demo (Let's deal with cancellation)

  38. Summary

  39. https://twitter.com/rakyll/status/869762601642799104

  40. Recap • GO has a rich standard library • Organize

    application by domain • Errors must be programmer friendly • ctx.Context Package ◦ WithValue must store request scoped values (e.g: CorrelationId) ◦ Provides cancellation
  41. Thank you You can find me at: • marcotalento90@gmail.com •

    https://twitter.com/Talento90 • https://github.com/Talento90
  42. Questions?