22
LET’s GO!!

DocumentGo

Embed Size (px)

Citation preview

Page 1: DocumentGo

LET’sGO!!

Page 2: DocumentGo

AIMED TASTING GO, NOT AIMED TO TEACH

NOT AN EXPERT OF GO

Page 3: DocumentGo

WEB Programming

Mobile Programming

Ali PARMAKSIZ

@parmaksiza Senior Developer at i2i systems

telecom, finance, electronics etc.

j2ee, gwt, android, dart, javascript, etc.

Page 4: DocumentGo

What?Why?

When?

Page 5: DocumentGo

Canonical, BBC

Heroku, CloudFoundry

SoundCloud, Mob Rules Games, Carbon Games

Google

Some Companies .....

Page 6: DocumentGo

IntelliJ Eclipse Netbeans

LiteIDE TextMate Komodo

Page 7: DocumentGo

Boolean, String

Numeric

Array, Slice, Map

Channel, Interface, Struct, Pointer, Function

Some Data Types...

Page 8: DocumentGo

func variables() (variableResult int) { //Explicit type declartion var var1, var2,

var3 int = 1, 2, 3

//The type is implied var4, var5, var6 := 4, 5, 6

//variableResult is a named //result parameter variableResult = var1 + var2 +

var3 + var4 + var5 + var6

//Don't need to return anything since variableResult was //assigned above.

return }

type vertex struct { x, y int } func pointers() { vertexOrignal := vertex{1, 2}

//This is going to make a copy of the original. vertexCopy := vertexOrignal

vertexCopy.x = 3 vertexCopy.y = 4 //vertexOrignal.x, vertexOrignal.y will

remain //unchange since it mutated the //copy

//This will assign a pointer to //vertexReference instead of //copying the

//value. vertexReference := &vertexOrignal vertexCopy.x = 3 vertexCopy.y = 4

//vertexOrignal.x, vertexOrignal.y will change //since it mutated the the pointer. }

SLICES

Variables Pointers

type vertex struct { x, y int

}

Struct

Page 9: DocumentGo

func sliceLength() { //Create slice with length of fivesliceWithLength := make([]vertex, 5)

for i := 0; i < len(sliceWithLength); i++ { sliceWithLength[i] = vertex{i, i + 1} }

}

func sliceNil() { //Create a nil slice. Used in the cases where you get //the size at runtime.

var sliceWithNil []vertex

for i := 0; i < 5; i++ { sliceWithNil = append(sliceWithNil, vertex{i, i + 1}) }

}

func mutatingSlices() { // This function mutates slicessliceVertices := make([]vertex, 5)

for i := 0; i < len(sliceVertices); i++ { sliceVertices[i] = vertex{i, i + 1} } //Won't change the //value in the slice

for _, vertex := range vertices { vertex.x++ vertex.y++ }

//Will change the value in the slice for i := 0; i < len(vertices); i++ { vertex := &vertices[i] vertex.x++ vertex.y++ } }

SLICES

Page 10: DocumentGo

func main() { elements := map[string]map[string]string{ "H": map[string]string{ "name":"Hydrogen", "state":"gas", }, "He":

map[string]string{ "name":"Helium", "state":"gas", }, "Li": map[string]string{ "name":"Lithium", "state":"solid", }, "Be": map[string]string{ "name":"Beryllium", "state":"solid", }, "B": map[string]string{ "name":"Boron", "state":"solid", }, "C": map[string]string{ "name":"Carbon", "state":"solid", }, "N": map[string]string{ "name":"Nitrogen", "state":"gas", }, "O": map[string]string{ "name":"Oxygen", "state":"gas", }, "F": map[string]string{ "name":"Fluorine", "state":"gas", }, "Ne":

map[string]string{ "name":"Neon", "state":"gas", }, } if el, ok := elements["Li"];

ok { fmt.Println(el["name"], el["state"]) } }

MAPS

elements := make(map[string]string) elements["H"] = "Hydrogen"

elements["He"] = "Helium" elements["Li"] = "Lithium"

elements["Be"] = "Beryllium" elements["B"] = "Boron"

elements["C"] = "Carbon“ elements["N"] = "Nitrogen"

elements["O"] = "Oxygen“ elements["F"] = "Fluorine" elements["Ne"] = "Neon"}

Page 11: DocumentGo

Don't communicate by sharing memory; share memory by communicating

Go's philosophically, to concurrency differs from the traditional use of threads and shared memory can be summarized as

Page 12: DocumentGo

Concurreny model in GO

Channels

Go routines

So what is channels and Go routines ?

Page 13: DocumentGo

Coming .......Go routines

Page 14: DocumentGo

Type safe pipe communicating different parts of the program

A SİNGLE CHANNEL CAN BE READ AND WRİTTEN BY GO ROUTİNES

A CHANNEL ALLOWS GO ROUTİNES TO COMMUNİCATE EACH OTHER

What is a channel ?

Page 15: DocumentGo

//Declaring and initializing..var c chan int

c= make (chan int) //or

c:=make(chan int)

//Send value to channelc<-1

//get value from channelvalue<-c

Page 16: DocumentGo
Page 17: DocumentGo
Page 18: DocumentGo
Page 19: DocumentGo
Page 20: DocumentGo
Page 21: DocumentGo

Multiple input

Multiple output

Failure

FUN TO USE İT

As a result of channel and go routines ....

Page 22: DocumentGo

Set it up to make 5 request a second and stop after 15 request were made. It's passing both web services 40 for the number of iterations to do, a CPU-intensive taking about 2 seconds to process

Node.js starts off by finishing the first four request simultaneously in about 4 seconds. Then it takes 3 seconds to finish one request at a time. It takes a total of nearly 34 seconds to complete all the requests

Go takes 2 seconds to finish 2 requests, another 4 to do 4 more and then down to 2 again and so on. It finishes all of them in about 6.5 seconds

See more at: https://c2fo.com/insights/exploring-googles-go-programming-language/