$30 off During Our Annual Pro Sale. View Details »

Creating a microservice for the 1st time with go

Creating a microservice for the 1st time with go

Henrique Vicente

May 08, 2015
Tweet

More Decks by Henrique Vicente

Other Decks in Programming

Transcript

  1. Creating a microservice
    for the 1st time with go
    Henrique Vicente

    View Slide

  2. Go is an open source programming language that makes it
    easy to build simple, reliable, and efficient software.
    golang.org
    Robert Griesemer, Rob Pike and
    Ken Thompson started sketching
    the goals for a new language on
    the white board on September 21,
    2007.

    View Slide

  3. Go is a programming language
    designed by Google to help solve
    Google's problems, and Google
    has big problems.
    https://talks.golang.org/2012/splash.article

    View Slide

  4. View Slide

  5. An attempt to combine the
    ease of programming of an
    interpreted, dynamically
    typed language with the
    efficiency and safety of a
    statically typed, compiled
    language.

    View Slide

  6. Influenced by
    • C family (basic syntax)
    • Pascal / Modula / Oberon family (declarations,
    packages)
    • Newsqueak, Limbo (concurrency)
    • Unix

    View Slide

  7. Design
    • Multi-value returns
    • No exception handling. Pleasant error handling.

    http://blog.golang.org/error-handling-and-go
    • Gourotines instead of threads
    • Object-oriented? Yes and no.

    No type hierarchy. Interfaces.
    • No pointer arithmetic for safety:

    no memory illegal address risk
    • Garbage collected

    View Slide

  8. View Slide

  9. View Slide

  10. Quality tools
    • go fmt
    • go lint
    • go vet
    • go test
    • go tool cover

    View Slide

  11. go get github.com/henvic/picel
    • Programming time?

    View Slide

  12. View Slide

  13. • Package main: executable program
    • Others: shared libraries
    • Imports:

    - don’t use relative internally

    - use the path to where the repo is published
    • Subpackages = subdirectories

    View Slide

  14. View Slide

  15. View Slide

  16. No official package
    manager
    • The designers of the language decided not to
    do it for a reason.
    • But what if you want to guarantee a specific
    package version?


    https://code.google.com/p/go-wiki/wiki/
    PackageManagementTools

    View Slide

  17. many alternatives
    • godeps
    • gpm: https://github.com/pote/gpm

    View Slide

  18. Building
    • go build
    • You get a single binary by default.
    • gox tool to build to multiple platforms easily

    or just use https://gobuilder.me/

    View Slide

  19. The go way
    • Unused variable?
    • Unused import?
    • Unused?


    = No compiling :( … :)
    • Just discovered: fix it automagically :)

    http://godoc.org/golang.org/x/tools/cmd/goimports

    Productivity += 1000
    • godoc -http=:6000 to see the docs on your computer or use
    godoc.org

    View Slide

  20. View Slide

  21. View Slide

  22. View Slide

  23. $GOPATH
    • Go likes to have all your src, pkg, bin inside a
    $GOPATH
    • But don’t worry. This doesn’t matter for end users
    of your application, given you’re release
    compiled binaries.

    View Slide

  24. View Slide

  25. Useful packages, methods
    • fmt.Println(“like console.log”)
    • fmt.Sprintf(“%+v is %v old”, person,
    person.Age())
    • flag: ./cmd -backend “https://google.com/”

    Flag: backend, value: https://google.com/
    • number, err := strcov.Atoi(“2442”)

    number is 2442 (int), err is nil

    View Slide

  26. OOP: image.Name()

    with multiple return params
    Code: https://goo.gl/Ma2ITn

    View Slide

  27. View Slide

  28. “Dockerifying
    • Docker is built in go
    • We’ve a microservice that is designed to work
    with default options
    • Why not build a docker container for it?

    View Slide

  29. Dockerfile
    FROM golang:onbuild
    MAINTAINER Henrique Vicente
    RUN apt-get update
    RUN apt-get install -y \
    imagemagick \
    webp
    EXPOSE 8123

    View Slide

  30. Docker Hub Registry
    • Add repo for automatic building on Docker Hub
    Registry to have your package certified
    • You can install with

    docker pull henvic/picel
    • See also Dockerfile.dev

    View Slide

  31. Some nice go projects
    srvdir

    View Slide

  32. Questions?

    View Slide