Go-Lang Hands-On| Part-3

  • Memory Management with Go-Lang.
  • Variable Addressing Scheme.
  • Pointer Notations.
  • Arrays in Go-Lang.
  • Slices in Go-Lang.
  • Unordered Map in Go-Lang.
  • Making Map an ordered Map.
  • Custom DataTypes.
  • Control Flows.
  • Break Loop flows.
  • Goto Statements.
  • Functions in GoLang.
  • Reading a File from Local Directory.
  • Generating an output File from Local Directory.
  • Scrapping data from Internet / Website.
  • Parsing the text data into JSON.
  • new() → With this function, memory is allocated, but it doesn’t initialise memory. When you allocate an object using the new() function, you’ll get back a memory address indicating the location of the complex data-type(e.g. map), but the data-type object itself has zeroed memory storage. The implication of this Zeroed Memory Storage is that : If you try to add a key-value pair to the map, it’ll cause an error.
  • make() → With this function, memory is both allocated as well as initialised too. You’ll get back that memory address, just like you do with new(), but the storage is non-zero and is ready to accept values.
  • The first line declares a map object and says that the keys are strings and the associated values are integers. And I’m declaring that with the new() function.
  • On the next line, I try to add a key value pair. And then I try to output the contents of the map, but I’m not going to get to that third line of code. Instead, the app will crash and I’ll see a panic error. It’s telling me that, I’m dealing with a map with zero memory storage.
  • There’s just no place to put the data, because memory is zeroed with new() function. And so there’s a panic and the application crashes.
  • Here, we have wrapped our declaration in the make() function which does allocates as well initialise the memory too.
  • In this version of the code, I’m once again saying that it’s a map object that contains strings as keys and int as values, but this time, I initialise with make(), and now when I try to add an entry, it’ll succeed. And when I output it, I get this output.
  • So, whenever you use these complex objects, it’s critical that you wrap the initialisation in the make() function, if your intent is to immediately add data to the object.
  • Note that, we have declared a Slice at line #8 with the help of make function.
  • Also note that, the initial capacity of the array has been declared as 5.
  • At line #16, we are appending more element at the end.
  • The garbage collector runs in the background and each time it kicks in, it looks for objects that are out of scope OR set to nil, so it can clean out the memory and return that memory to your memory pool.
  • The garbage collector was completely rebuilt in Go 1.5 for very low latency to reduce the pauses that happen when you’re running Go applications. And now, the process for de-allocating memory goes by so quickly that you’re very unlikely to notice it, even on slower computers.
  • Demo #1 Here, *int indicates that, thisVar is a pointer and not value. And if I don’t assign anything, that means that, variable will be nil; it doesn’t contain anything.
  • Demo #2 Here, we are printing the *thisVar, which indicates that, thisVar is a pointer and not value. This time, it throws the RuntimeError indicating the InvalidMemoryAddress.
  • Here, variable “thisParticularInVar” is valued at 75.
  • The Pointer-Variable “thisPointerVar” is pointing to the address of variable thisParticularInVar.
  • The notation “&thisParticularInVar” is pointing to the address of variable thisParticularInVar.
  • The notation “*thisPointerVar” is referring to the actual value being stored at address stored in thisPointerVar.
  • Notice that, at line #8, we specifically want the type of the variable to be inferred. Here, we want that GO-compiler should be auto-inferring the type of 75.
  • Note that, At line #14, we modified the value of the variable ‘thisParticularInVar’ through it’s pointer variable.
  • The change made through the pointer variable is very well reflecting in the original variable too.
  • We have declared an Array of Strings at line #8. This is an Array of 4 Strings have been initialised.
  • At Line #9, the array of 5 integer values have been explicitly initialised and in subsequent lines, we are assigning values to each array index. Note that, we are just using the “=” symbol i.e. assignment operator, because we don’t need to infer the type anymore.
  • Also, we have demonstrated the usage of “len” method, to showcase the length of the array here. Recall that, this method “len” comes from a special package named builtin. The Go compiler assumes that the builtin package is always imported.
  • Note that, at line #14, we are trying to add more entries tot he Array earlier declared. And we can add infinite elements, there is no constraint of size here.
  • Also observe that, at line #16, we are performing the sorting operation of the Array Slice.
  • Note that, we are using make() function at line #8, in order to initialise the Map.
  • At line #8, we have defined the Map with both <K,V> as of string type.
  • At line #16, we are also performing the delete operation basis of key.
  • At line #17 to 19, each value of a struct is technically known as a field.
  • At line #10, we have printed the entire custom object and it’s printing the name of the attribute as well as it’s corresponding value.
  • At line #12, we are referring the attributes through object access.
  • Notice that, we can’t use angle-brackets for putting the conditions for check.
  • Also note that, the curly braces have to be on the same line. From Java, we can even write the starting curly braces in the next line as well.
  • We can use a package called math/Rand and another package the time package that we’ve seen before. From the rand package, I’m calling the seed function and passing in the current time in Unix format.
  • Then I’m using a function called Intn and passing in a ceiling of seven, and then adding one, and this will give me a number between one and seven. Each time I run the application, I’ll get possibly a different number.
  • And it all depends on the millisecond of the current time on my computer. It is possible to see the same number over and over again. But that’s just coincidence.
  • break → It breaks the flow of the code and ends the loop.
  • continue → It just restarts the flow of the code from the beginning.
  • This method can receive infinite values in input.
  • This method would return ONE value of int type.
  • Here In Go-Lang, we can attach functions to your custom types. They’re then referred to as methods.
  • In a more completely Object Oriented language such as Java, you’d say that each method is a member of a class whereas In Go, a method is a member of a type.
  • So what do we think is going to happen? Will the dog bark three times, or nine times? And the answer is that it only barks three times.
  • That’s because, when you pass the dog object in as the receiver, a copy is made of it, it’s not a reference. If you want it to be a reference, you can use pointers. But within the function, this is a brand new copy of the object, so when I modify the sound field, I’m not modifying the version that was created in the main function.
  • Here @ line #13, we are specifying the name of the file, that we are planning to generate.
  • Here @ line #16, we are suggesting to generate a file with data being stored in the variable “content”.
  • In the output section below, it’s evident that a output file has been generated well.
  • Here @ line #19, keyword “defer” is very important, as it shall wait for everything, untill this line executes.
  • Here @ line #14, we are specifying the name of the file, from which we are planning to read the data.
  • Here @ line #20, note that, data being read from the file is always in BYTES format and we need to convert the same to string format, to display the same in human readable format.
  • Here @ line #12, we are using the http.Get call in order to receive the data from aforementioned URL.
  • Here @ line #17, we are reading the response thus received in the “resp” variable.

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How To Use A Dolly Track In Unity

Rearrange PDF Pages Using Java

3 Important Content-Type Headers for Flutter

Solving Candy Crush with Special Blocks, i.e., Match-3 games with swaps

Tag propagation released to general availability

ASP .NET Core with Docker: Beginers Guide

Late Goodbye to 2018

https://medium.com/m/callback/email?token=176d3d4fed92&operation=register&state=medium&source=email-

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

In Conversation with Go-Lang’s DataTypes | Part2

Learning Go from Documentation #3 (Error handling)

Go(lang) playground