Upload
ali-parmaksiz
View
77
Download
0
Embed Size (px)
Citation preview
LET’sGO!!
AIMED TASTING GO, NOT AIMED TO TEACH
NOT AN EXPERT OF GO
WEB Programming
Mobile Programming
Ali PARMAKSIZ
@parmaksiza Senior Developer at i2i systems
telecom, finance, electronics etc.
j2ee, gwt, android, dart, javascript, etc.
What?Why?
When?
Canonical, BBC
Heroku, CloudFoundry
SoundCloud, Mob Rules Games, Carbon Games
Some Companies .....
IntelliJ Eclipse Netbeans
LiteIDE TextMate Komodo
Boolean, String
Numeric
Array, Slice, Map
Channel, Interface, Struct, Pointer, Function
Some Data Types...
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
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
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"}
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
Concurreny model in GO
Channels
Go routines
So what is channels and Go routines ?
Coming .......Go routines
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 ?
//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
Multiple input
Multiple output
Failure
FUN TO USE İT
As a result of channel and go routines ....
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/