https://slides.com/xergioalex

LET'S BUILD TOGETHER

http://rocka.co

xergioalex

Sergio A. Florez

TECH LEAD && FULL STACK DEVELOPER

What is Go?


LET'S BUILD TOGETHER

LET'S BUILD TOGETHER

What is Go?

Open Source

+

LET'S BUILD TOGETHER

What is Go?

go build

Compiled (Cross-Compile)

&& Multiplatform

LET'S BUILD TOGETHER

What is Go?

C Sintax

C Sintax (Interpreted language styles)

package main

import "fmt"

func main () {
    fmt.Printf("Hello World")
}

LET'S BUILD TOGETHER

What is Go?

C Sintax

C Sintax (Interpreted language styles)

package main

import "fmt"

func main () {
    fmt.Printf("Hello World")
}

Why Go?

 

LET'S BUILD TOGETHER

LET'S BUILD TOGETHER

Why Go?

Easy To Learn

Why Go?

Modern language

Designed thinking on modern systems that needs:

- High performance

- High concurrency

LET'S BUILD TOGETHER

Why Go?

The Standard Library

- net/http

- database/sql/driver

- encoding

- testing

- encryption

LET'S BUILD TOGETHER

Why Go?

Gopher Pet :3

LET'S BUILD TOGETHER

History

Go started in 2007 by

- Robert Griesemer (JVM, V8 Js)

- Rob Pike (UNIX, UTF-8 )

- Khen Thompson

  (B, C, UNIX, UTF-8)

LET'S BUILD TOGETHER

LET'S BUILD TOGETHER

History

- Go was released by Google as an open source project in 2009

- Version 1.0 was released in 2012.

- Go team didn't seek to replace Java, C ++ or any other language, but instead devoted themselves to developing a system that would give them many advantages they were looking for.

LET'S BUILD TOGETHER

Who use Go?

- Canonical
- CloudFlre
- Uber

- Disqus
- Digital Ocean
- Facebook
- Netflix

LET'S BUILD TOGETHER

Thrends

Most Wanted

CODING TIME

LET'S BUILD TOGETHER

Linux Installation

$ sudo add-apt-repository ppa:longsleep/golang-backports
$ sudo apt-get update
$ sudo apt-get install golang-go
$ go version
go version go1.12 linux/amd64

LET'S BUILD TOGETHER

DEMO

Hello World

package main

import "fmt"

func main () {
    fmt.Printf("Hello World")
}

LET'S BUILD TOGETHER

DEMO

Standard input

package main

import "fmt"

func main () {
    var name string
    fmt.Print("Please type your name: ")
    fmt.Scanf("%s", &name)
    fmt.Printf("Welcome %s to Go Lang", name)
}

LET'S BUILD TOGETHER

DEMO

Declarations

var name string
var name string = "Name test"
lastname := "Lastname test"
var number = 100
var number1 int
var (
    a = 1
    b = 2
    c = 3
)

DEMO

Types

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128

DEMO

Functions 01

func test() bool {
    return true
}

func test1() int32 {
    return 5
}

func test1(myVar1 int32, myVar2 float32, myVar3 string) int32 {
    // ...
    return 1000
}

FUNC

DEMO

Functions 02

a, b, c := getVariables()

func getVariables() (int a, int, int) {
    return 1, 2, 3
}

// ...
val1, val2, val3, val4 = calc(num1, num2)

func calc(a int32, b int32) (int32, int32, int32, float32) {
  return a + b, a - b, a * b, float32(a) / float32(b)
}

DEMO

Loops 01

# Type 1
for i := 0; i < 5; i++ {
    fmt.Println("FOR: ", i)
}

# Type 2
c:=100
for ; c > 0; {
    c -= 10
}

DEMO

Loops 02

# Type 3
c:=100
for c > 0 {
    c -= 10
}

# Type 4
s := 1000
for {
    s -= 1
    if s == 0 {
        break
    }
}

LET'S BUILD TOGETHER

DEMO

Conditionals

var number = 0
fmt.Print("Typ a number: ")
fmt.Scanf("%d", &number)

if number %2 == 0 {
    fmt.Println("Even number")
} else {
    fmt.Println("Odd number")
}

DEMO

Switch 01

switch number {
    case 1:
        fmt.Println("The number is 1")
    default:
        fmt.Println("The number isnt 1")
}

//...
switch {
    case number%2 == 0:
        fmt.Println("The number is even")
    default:
        fmt.Println("The number is odd")
}

DEMO

Switch 02

switch {
    case number == 2:
        fmt.Println("The number is 2")
	fallthrough
    case number%2 == 0:
	fmt.Println("The number is even")
    default:
        fmt.Println("The number is odd")
}

fallthrough

DEMO

Arrays 01

var arr1 [2]string

arr1[0] = "value1"
arr1[1] = "value2"
// arr1[2] = "value2" // This will 
                      //throw exception
fmt.Println(arr1)

DEMO

Arrays 02

b := [5]int{1, 2, 3, 4, 5}

fmt.Println("My array:", b)

DEMO

Arrays 03

var twoD [2][3]int

for i := 0; i < 2; i++ {
    for j := 0; j < 3; j++ {
        twoD[i][j] = i + j
    }
}

fmt.Println("2d: ", twoD)

DEMO

Maps 01

mapTest := make(map[string] int)

mapTest["key1"] = 1
mapTest["key2"] = 100

fmt.Println(mapTest)

DEMO

Maps 02

mapTest := map[string] int {
    "Juan": 18,
    "Sergio": 24,
    "Julian": 30,
}

delete(mapTest, "Juan")

fmt.Println(mapTest)

DEMO

Structs 01

type Course struct {
    Name string
    Slug string
    Skills [] string
}

myCourse := Course{ 
    Name: "Go", 
    Slug: "go", 
    Skills: []string{"1", "2"} 
}

DEMO

Structs 02

type Course struct {
    Name string
    Slug string
    Skills [] string
}

myCourse := new(Course)

myCourse.Name = "Go2"
myCourse.Slug = "go2"
myCourse.Skills = []string{"3", "4"}

DEMO

Methods

type Course struct {
    Name string
    Slug string
    Skills [] string
}

func (p Course) Subscribe (name string) {
    fmt.Printf("The person %s has subscribed to %s course\n", name, p.Name)
}

myCourse := Course{ Name: "Go", Slug: "go", Skills: []string{"1", "2"} }
myCourse.Subscribe("XergioAleX")

fmt.Println(myCourse)

DEMO

Interfaces

type Shape interface {
    Area() float64
    Perimeter() float64
}

type Rect struct {
    width float64
    height float64
}

func (r Rect) Area() float64 {
    return r.width + r.height
}

func (r Rect) Perimeter() float64 {
    return 2 * (r.width + r.height)
}

var s Shape
s = Rect{5.0, 4.0}

fmt.Print("Area", s.Area())
fmt.Print("Perimeter", s.Perimeter())

DEMO

Defer 01

func deferTest () {
    fmt.Println("The main function finish")
}

func main() {
    defer deferTest() // defer allow do something after the current function context finish

    fmt.Println("Hello world")
}

DEMO

Defer 02

func orderDefer() { // Defers works as stack
    fmt.Println("Start")
    defer defered("1")
    defer defered("2")
    defer defered("3")
    defer defered("4")
    defer defered("5")
    fmt.Println("End")
}


orderDefer()

DEMO

Panic / Errors

func main() {
    number, err := Div(50, 0)

    if err != nil {
        panic(err)
    }

    fmt.Println(number)
}
func Div(a float32, b float32) (float32, error) {
    if b == 0 {
        return 0, errors.New("Division by 0")
    }

    return a / 2
}

DEMO

Pointers

func main() {
    a := 100
    var b * int

    b = &a
    fmt.Println(a, *b)
    fmt.Println(&a, b)

    *c = 10
    fmt.Println(a, *b)
    fmt.Println(&a, b)
}

DEMO

GoRoutines 01

func helloGo(index int) {
    fmt.Printf("Hello, the index is #%d\n", index)
}

func forGo(n int, text string) {
    for i := 0; i < n; i++ {
        helloGo(i)
    }
}

func main () {
    forGo(500)
}

...

DEMO

Go Routine 02

func helloGo(index int) {
    fmt.Printf("Hello, i am a goroutine #%d\n", index)
}

func forGo(n int, text string) {
    for i := 0; i < n; i++ {
	   helloGo(i)
    }
}

func main () {
    forGo(500)
    
}

go

time.Sleep(1000 * time.Millisecond);

DEMO

Channels

ch := make(chan string)

go func () {
    defer close(ch)
    ch <- "Hi Channel"
}()

fmt.Println(<- ch)

DEMO

Server

package main

import (
    "net/http"
    "io"
)

func handler (w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "Hola Servidor Go!")
}

func main () {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8000", nil)
}

DEMO

Http Client

import "net/http"

type Post struct {
    UserID int    `json:"userId"`
    ID     int    `json:"id"`
    Title  string `json:"title"`
    Body   string `json:"body"`
}

func main () {
    var client = &http.Client{ Timeout: 10 * time.Second }
    url := "http://jsonplaceholder.typicode.com/posts"
    resp, err := client.Get(url)
    //...
    var post [] Post

    json.NewDecoder(resp.Body).Decode(&post)

    fmt.Println(len(post))
    fmt.Println(post[0])
}

DEMO

Files

package main

import (
    "fmt"
    "io/ioutil"
    "path"
    "runtime"
)

func main() {
    // Get information about program execution
    _, filename, _, ok := runtime.Caller(0)
    // Validate execution
    if !ok {
	panic("No caller information")
    }
    /* Read a file */
    data, err := ioutil.ReadFile(path.Dir(filename) + "/test.txt")
    // Stop execution if error any error happen
    if err != nil {
	panic(err)
    }
    // Print file content
    fmt.Println(string(data))
}

LET'S BUILD TOGETHER

I Know Go

Frameworks

Most used

LET'S BUILD TOGETHER

Libraries

Most Used

LET'S BUILD TOGETHER

> Ebiten

Simple 2D game library

LET'S BUILD TOGETHER

BONUS

LET'S BUILD TOGETHER

Links

LET'S BUILD TOGETHER

Friends of Go

LET'S BUILD TOGETHER

CodeLyTV

LET'S BUILD TOGETHER

Platzi

FINAL BONUS

LET'S BUILD TOGETHER

http://rocka.co

xergioalex

Sergio A. Florez

TECH LEAD && FULL STACK DEVELOPER