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

Creating a microservice for the 1st time with go

Creating a microservice for the 1st time with go

Henrique Vicente

May 08, 2015

More Decks by Henrique Vicente

Other Decks in Programming


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

  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.
  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
  4. None
  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.
  6. Influenced by • C family (basic syntax) • Pascal /

    Modula / Oberon family (declarations, packages) • Newsqueak, Limbo (concurrency) • Unix
  7. Design • Multi-value returns • No exception handling. Pleasant error

 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
  8. None
  9. None
  10. Quality tools • go fmt • go lint • go

    vet • go test • go tool cover
  11. go get github.com/henvic/picel • Programming time?

  12. None
  13. • Package main: executable program • Others: shared libraries •

 - don’t use relative internally
 - use the path to where the repo is published • Subpackages = subdirectories
  14. None
  15. None
  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
  17. many alternatives • godeps • gpm: https://github.com/pote/gpm

  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/
  19. The go way • Unused variable? • Unused import? •

 = No compiling :( … :) • Just discovered: fix it automagically :)
 Productivity += 1000 • godoc -http=:6000 to see the docs on your computer or use godoc.org
  20. None
  21. None
  22. None
  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.
  24. None
  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
  26. OOP: image.Name()
 with multiple return params Code: https://goo.gl/Ma2ITn

  27. None
  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?
  29. Dockerfile FROM golang:onbuild MAINTAINER Henrique Vicente <[email protected]> RUN apt-get update

    RUN apt-get install -y \ imagemagick \ webp EXPOSE 8123
  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
  31. Some nice go projects srvdir

  32. Questions?