Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From nil to Gopher

From nil to Gopher

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

Marco Talento

October 24, 2018
Tweet

More Decks by Marco Talento

Other Decks in Programming

Transcript

  1. 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)
  2. 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...)
  3. Requirements • Building something useful • Try to use most

    of the go features • Good DX (Developer experience) • Testing and Quality
  4. IMGART • HTTP service for image manipulation • Configurable profiles

    • Image Caching • Source Code: https://github.com/Talento90/imgart
  5. Question and Decisions • How should I organize my application?

    • Error Handling? • How to Debug? • How to maintain code quality? • Should I care about cancellation?
  6. 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
  7. Structure Application • Group packages by domain • Dependencies Inwards

    • No circular dependencies • Isolate external dependencies
  8. 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)
  9. 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
  10. 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)
  11. 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
  12. 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)
  13. 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
  14. 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
  15. Thank you You can find me at: • [email protected]

    https://twitter.com/Talento90 • https://github.com/Talento90