Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

WHY GO?

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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)

Slide 5

Slide 5 text

Learning GO https://bit.ly/2pOQT8J

Slide 6

Slide 6 text

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...)

Slide 7

Slide 7 text

Let's build something https://golang.org/doc/gopher/pencil/

Slide 8

Slide 8 text

Requirements ● Building something useful ● Try to use most of the go features ● Good DX (Developer experience) ● Testing and Quality

Slide 9

Slide 9 text

IMGART

Slide 10

Slide 10 text

IMGART ● HTTP service for image manipulation ● Configurable profiles ● Image Caching ● Source Code: https://github.com/Talento90/imgart

Slide 11

Slide 11 text

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 ++ + + =

Slide 12

Slide 12 text

Where do I start?

Slide 13

Slide 13 text

Question and Decisions ● How should I organize my application? ● Error Handling? ● How to Debug? ● How to maintain code quality? ● Should I care about cancellation?

Slide 14

Slide 14 text

How to structure GO applications?

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Structure Application ● Group packages by domain ● Dependencies Inwards ● No circular dependencies ● Isolate external dependencies

Slide 17

Slide 17 text

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)

Slide 18

Slide 18 text

Error Handling https://bit.ly/2d0qz5C

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Errors in GO https://golang.org/src/errors/errors.go

Slide 21

Slide 21 text

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)

Slide 22

Slide 22 text

Example: Handle Errors to Status Codes

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Testing and Quality Tools

Slide 28

Slide 28 text

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)

Slide 29

Slide 29 text

Table testing https://github.com/golang/go/wiki/TableDrivenTests

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Why cancellation matters?

Slide 32

Slide 32 text

Request Lifecycle Request Lifecycle Download Image Input Process Image Response User Cancel Request

Slide 33

Slide 33 text

context.Context https://golang.org/pkg/context/

Slide 34

Slide 34 text

context.Context Errors

Slide 35

Slide 35 text

ctx.Context Example

Slide 36

Slide 36 text

HTTP Requests (Download Image)

Slide 37

Slide 37 text

Demo (Let's deal with cancellation)

Slide 38

Slide 38 text

Summary

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Thank you You can find me at: ● [email protected] ● https://twitter.com/Talento90 ● https://github.com/Talento90

Slide 42

Slide 42 text

Questions?