Learning GO to get the minimum latency and get things right from start ..

Go docs
Go Tour

The whole file needs to become a package and to make it whole a package we wrap that up in package main and this makes the whole code as a single package that is then converted to binary

We create package to help us import those in other files and the importing helps in code seperation, else we would have to write the whole code in a single main.go file..

Imports

The internal imports are done using:

imp"""ofngrmeitttt"/h(hutbt.pc"om/gorilla/mux"

so these are the imports as we made in the python and to get these imported sorted we have to take 2 steps

12..``ggoommoodditniidty`<p:actkhaigse-insamlei>k`e:picpaninbsetanlalme-drarn.yttxhtinwgh,erweeinneepdytthhoinswteowmraikteestuhreeltihberamroydunlaemeissigno.rm.otdxtanhderpeubwleicwraintdeatnhyeonleibcraanrideoswnilnoacdodaenditusseelfthaendfuwnecttihoenysg/etcslaisnssetsallliesdteadndinadidtedasrequireinthemodfile

func main() is the function that gets called by default as soon as we compile the app

go run . : runs the file

Multiple modules

If each module in a separate directory, you will need to basically convert that package from external link to the local one :

~/go-gadrpeepve//tgggaioropn.e.pgmem.stogdidon(g(msmo.odgduoulleeeexxaammppllee..ccoomm//garpepe)tings)

So to use greetings.go in app.go we need to import in this way in the go mod edit --replace example.com/greeting=../greetings

also the package name should be same as the directory name ..

Variable declaration

vnnaaarmmeena=:m=e"M"soHtherilitln"og"/Herethetypedeclarationistakenfromtherghtsideofthestring

list declaration:

m}es"""sHHHaiiige123""":,,,=[]string{

Error Handling

send the value , error with it ..

so the return is a tuple ( message , error-code ) , error return nil means no error , else send an erorr using :

iimfpronerattmuer"ne=r="r"o",r"s:e"rrors.New("emptyname")

Range keyword

used to iterate over

ASSMCrltahrirpaacisnyen,n,sge,sl,s

Make

map_init := make(map [string]int ) slice_init := make([]int, 5) chan_init := make(chan int)

GoRoutines

goroutines are like threads, assume that they are threads and you can goroutines will work, new goroutines are created using the ‘go’ keyword (prefix) , any function can become a goroutine just add the go keyword in front and doesnt require function coloring

Channels

channels are way of communicating before 2 goroutines, data transfer between goroutines are done using these channels,

Each channel is a has a particular type of data-structure / data-type that is called Element type

chs:o=imtaskea(cihnatnegienrt)datachannelmeansintegerscanonlypassedthroughthischannels

make : this keyword is used to 3 datatypes (map , channel , struct)

cxh-=ch-xchar/eaacesrieevncedeissvtteaattseetmmaeetnnettm;enrtesiusltanisasdsiisgcnamrednetd

Channels support close keyword , which sets a flag indicating that no more values will ever be sent on this channel ..

Example :

pif}am"uns/g}/g}/f}cpfnaq/of}c(/of}c(/ofckomgctuonl)osl)rmhartouaNfraoSfrqoPtagt"mrrautsquusrx.nedaaetnxueunxaei:Pn(oilsuc:r(acr(n=remensr(=anr(:estilas(:a)lae)=sqernsin):=l{0sts{uratnt{=s;uranl,mrargnamaxanee(plak<lgxsxilke1xse*)s)poe(0)xqew(c0nulchaainha;trnaanueemnxrsei+adin+l{ntsft){{u)nctionsinsidethemain()functiononlyanonfunctionscanbedeclared

Unbuffered Channel

by default, the initialised channel is an unbuffered one , that is there is not buffer stored in the channel , once go routines sends a value , it will wait till receiver receives that value

ccchhh===mmmaaakkkeee(((ccchhhaaannniiinnnttt),,03))/uubnnubbfuufffeffreeerrdeeddchccahhnaannnennleellwithcapacity3

Called as Synchronous channel

Buffered Channel

It has a queue of elements, send inserts at the end , receiver takes out the value from the start, and if the queue is filled the sender no longer pushes the values in .. so that is one drawback

If in an unbuffered channel , goroutine is trying to enter value then it wont be able to add values to the buffer this situation is called goroutine leak