Go provides concurrency through lightweight threads called goroutines and synchronous communication through channels. Goroutines allow efficient multiplexing of multiple processes onto fewer OS threads. Channels allow goroutines to communicate and synchronize safely without shared memory or locks. This model of goroutines and channels makes it easy to write simple, scalable concurrent programs in Go.
8. 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
type vertex struct {
x, y int
}
Struct
//Don't need to return anything since
variableResult was //assigned above.
return }
Variables
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. }
Pointers
SLICES
9. func sliceLength() { //Create slice with length of five
sliceWithLength := 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 slices
sliceVertices := 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
15. //Declaring and initializing..
var c chan int
c= make (chan int)
//or
c:=make(chan int)
//Send value to channel
c<-1
//get value from channel
value<-c
22. 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/