LEARN GOLANG| Part-3

  • Memory Management with Go-Lang.
  • Usage of new() function with GoLang.
  • Usage of make() function with GoLang.
  • Usage of arrays with help of make() function.
  • Automatic Memory Management using Garbage Collector.
  • Concept of Resource in GoLang.
  • Freeing up resource by using defer keyword.
  • Proper closure of Body while making external http Call from Go.
  • Simple Error Handling in GoLang.
  • 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.
  • At line # 7, cleanup() is a function that will free your resource.
  • At line #12, We use a defer code, to free this resource, but note that, first the worker code is executed and only then the the defer’s code is executed. Defer will be work even if you have an error or a panic in your code, making sure that your resources will be freed, in any case.
  • What’s nice about defer is that you write it just after you acquire the resource. This way, you don’t forget to make sure it’s freed. Defer the code in reverse order.
  • At line #12, we are hitting to the external URL, using the http package. We are also catching the response and error from the same.
  • At line #16, we are well using the defer keyword, by the help of which, we indicate that we need to close the ResponseBody, post the processing of this particular method is done.
  • At line #17, we are getting the value of “Content-Type” and it’s an automatic type guessing here.
  • Here we have an sqrt function, which calculates the square root of a number. But, unlike the one in the math standard library this one will return error on negative numbers.
  • We can see from above method that, we return two values. One is float 64, which is the result, and the other one is of type error.
  • At line #10, we are returning zero, because we have to return some value for the float, but also we are returning a new error.
  • At line #13, we are returning the square root of n and nil. Nil is the value that Go uses to signal nothing. It is very much like null or none in other languages.

--

--

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