35
9/25/2016 Go Installfest http://localhost:3999/present.slide#34 1/35 Go Installfest 24 December 2016 Thach Le Triet Pham

Go Installfest

Embed Size (px)

Citation preview

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 1/35

Go Installfest24 December 2016

Thach LeTriet Pham

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 2/35

Who are we?

Programming addict

Java, NodeJS, Go

Backend at DF/Backend at Lozi

1.5 years with Go

runikitkat.com (http://runikitkat.com)

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 3/35

Goal

Help new gophers know what exactly they need to start using Go.

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 4/35

Agenda

Install, set up Go

Go commands + tools

Glide

Beego

Gin

Gorm

gRPC

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 5/35

Install, set up Go

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 6/35

Install Go

Using HomeBrew

Download from Homepage(https://golang.org/doc/install)

Install from source.

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 7/35

Set up Go

GOPATH

GOBIN

GOROOT

PATH

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 8/35

Go commands + tools

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 9/35

Commands

go build

go test

go run

go get

go fmt

go lint

go vet

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 10/35

Integrate with Editor/IDE

Vim: vim-go

Emacs: go-mode

Sublime: GoSublime

Atom: go-plus

VSCode: vscode-go

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 11/35

Hello world

package main import "fmt" func main() { fmt.Println("Hello world") }

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 12/35

Go import

package main import "github.com/k0kubun/pp" func main() { pp.Println("Hello world") }

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 13/35

Glide

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 14/35

Introduction

Cargo, npm, pip, Maven...

Packages manager

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 15/35

Install

curl https://glide.sh/get | sh

brew install glide

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 16/35

Usage

$ glide create # Start a new workspace $ open glide.yaml # and edit away! $ glide get github.com/Masterminds/cookoo # Get a package and add to glide.yaml $ glide install # Install packages and dependencies # work, work, work $ go build # Go tools work normally $ glide up # Update to newest versions of the package

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 17/35

Beego

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 18/35

Introduction

http://beego.me/

Fullstack web framework

RESTful, MVC model

High performance

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 19/35

Installation

go get github.com/astaxie/beego

go get github.com/beego/bee

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 20/35

Using

$ cd $GOPATH/src $ bee new hello $ cd hello $ bee run

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 21/35

Gin

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 22/35

Introduction

https://gin-gonic.github.io/gin/

Minimal framework - but use for API much better

Fast

Easy to use

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 23/35

Installation

go get github.com/gin-gonic/gin

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 24/35

Using

import "github.com/gin-gonic/gin"

package main import "github.com/gin-gonic/gin" func main() { // Creates a gin router with default middleware: // logger and recovery (crash-free) middleware router := gin.Default() router.GET("/someGet", getting) router.POST("/somePost", posting) router.PUT("/somePut", putting) router.DELETE("/someDelete", deleting) router.PATCH("/somePatch", patching) router.HEAD("/someHead", head) router.OPTIONS("/someOptions", options) // By default it serves on :8080 unless a // PORT environment variable was defined. router.Run() // router.Run(":3000") for a hard coded port }

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 25/35

Gorm

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 26/35

Introduction

ORM library

Developer friendly

Auto migration

Logger

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 27/35

Installation

go get -u github.com/jinzhu/gorm

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 28/35

Using

import "github.com/jinzhu/gorm"

import _ "github.com/lib/pq"

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 29/35

Code

type Product struct { gorm.Model Code string Price uint } func main() { db, err := gorm.Open("sqlite3", "test.db") if err != nil { panic("failed to connect database") } defer db.Close() db.AutoMigrate(&Product{}) db.Create(&Product{Code: "L1212", Price: 1000}) var product Product db.First(&product, "code = ?", "L1212") // find product with code l1212 db.Model(&product).Update("Price", 2000) db.Delete(&product) }

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 30/35

gRPC

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 31/35

Introduction

Distributed services and clients

Across languages

RPC

Protobuf

HTTP/2

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 32/35

Installation

go get google.golang.org/grpc

go get github.com/grpc/grpc-go

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 33/35

Using

De�ne a service in a .proto �le.

Generate server and client code using the protocol bu�er compiler.

Use the Go gRPC API to write a simple client and server for your service.

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 34/35

Thank you

Thach LeTriet [email protected] (mailto:[email protected])

[email protected] (mailto:[email protected])

9/25/2016 Go Installfest

http://localhost:3999/present.slide#34 35/35