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

Golang and basic Docker

Golang and basic Docker

This slide is made to share knowledge for Orcsoft company's employees. This session shared basic knowledge of Go programming (Golang) and how to implement by using various technology(ies) and framework the company use in a project for a customer.

#golang

Avatar for Natth Wetchapan

Natth Wetchapan

April 27, 2019
Tweet

Other Decks in Programming

Transcript

  1. Go programming and Basic Docker Orcsoft Co., Ltd. - internal

    training Natth Wetchapan Senior Software Engineer
  2. Syllabus - Why Go? - Needed tools for development -

    Basic Golang - Create a web service API - Go Module - Basic Docker
  3. Why Go? - History “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 go.org has already been used -> golang.org -> folks call it Golang
  4. Why Go? - History(con.) - Big (large scale of) hardware

    - Big (large scale of) software - Big variation of programming language (mostly in C++ and a lot in Java, and Python) - Big team of engineers
  5. Why Go? - in short "Go is a compiled, concurrent,

    garbage-collected, statically typed language developed at Google." - https://talks.golang.org/2012/splash.article
  6. Why Go? - History(con.) Ken Thompson B language, C language,

    UTF-8 credit information from: wikipedia.org credit image from: https://www.youtube.com/watch?v=sln-gJaURzk Rob Pike Unix, UTF-8 Robert Griesemer JVM Hotspot
  7. Needed tools for development 1. Install using brew $ brew

    install golang 2. Install by execution file https://golang.org/doc/install Setting environment ( Unix ) ( DOS ) export GOPATH=$HOME/go set GOPATH=c:\Users\%USERNAME%\go export PATH=$PATH:$GOPATH/bin set PATH=%PATH%;%GOPATH%\bin
  8. Needed tools for development(con.) Text Editor Code Editor Integrated Development

    Environment (IDE) GoLand by Intellij Goclipse by Eclipse Notepad Vim Visual Studio Code Sublime Text
  9. Package Basic Golang - primeRK int + ToLower(s string) string

    + ToUpper(s string) string - hasStr(sep string) (uint32, uint32) ... strings - signingMethods map[string]func() + Token string + RegisterSigningMethod(alg string, f func() SigningMethod) + GetSigningMethod(alg string) (method SigningMethod) ... jwt “+” Public method/variable “-” Private method/variable
  10. Basic Golang(con.) Function Declaration - Global variable declaration - Struct

    declaration - Function declaration - Interface declaration
  11. Basic Golang(con.) IF - ELSE statement - && (logical and

    operation) - || (logical or operation) - == (equal operation) - != (not equal operation)
  12. Basic Golang(con.) Array [N]Type [N]Type{value1, value2, ..., valueN} [...]Type{value1, value2,

    ..., valueN} Slice make([]Type, length, capacity) make([]Type, length) []Type{} []Type{value1, value2, ..., valueN}
  13. Create a web service API(con.) Hello, World! $ go get

    -u github.com/labstack/echo/… $ mkdir hello; cd hello $ touch main.go
  14. Create a web service API(con.) Middleware - echo.Pre() middleware which

    is executed before router processes the request - echo.Use() middleware which is executed after router processes the request, this middleware has full access to echo.Context API Learn more: https://echo.labstack.com/middleware
  15. Create a web service API(con.) Routing - GET Handle GET

    request method - POST Handle POST request method - Group Group a specific path of route
  16. Go Modules - Dependency - Version control - Develop anywhere

    For more information: https://github.com/golang/go/wiki/Modules ps. Go Module directory should be outside of $GOPATH Goodbye $GOPATH. Hello, Go Modules.
  17. Go Modules(con.) - $ go mod init - GO111MODULE=on go

    mod init (for older version) - $ go mod tidy - add missing and remove unused modules - $ go mod vendor - make vendored copy of dependencies - $ go mod download - download modules to local cache, can use $go clean to delete
  18. Basic Docker(con.) Docker Platform = Docker Engine + Docker Hub

    - Docker Engine - Docker Daemon - Docker CLI - Docker Hub
  19. Basic Docker(con.) Docker CLI - docker build # Build an

    image from a Dockerfile - docker images # List all images on a Docker host - docker run # Run an image - docker ps # List all running and stopped instances - docker stop # Stop a running instances - docker rm # Remove an instance - docker rmi # Remove an image
  20. Basic Docker(con.) File Structure httpserver - main.go - Dockerfile -

    go.mod - go.sum $ docker build -t httpserver . $ docker images
  21. Basic Docker(con.) Multi-state build - Reduce image size - Remove

    unnecessary files $ docker build -t httpserver_prod . $ docker run httpserver --name goservice -p 8080:80