LEARN GOLANG| Part-3

aditya goel
7 min readSep 24, 2022

--

In case you are landing here directly, it’s suggested that you go and read through this for fundamentals.

In this blog, we shall be looking at following concepts :-

  • 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.

Question:- How a GO application is being run ?

Answer:- When you run a Go application with the GO run command, you’re depending on the Go runtime that’s installed on your computer.

Question:- Is the GO runtime included with build ?

Answer:- When you compile and build a binary GO application, the runtime is included. Either way, your application depends on the runtime which operates in the background in dedicated threads.

Question:- How is the memory managed with GO-lang ?

Answer:- Like other managed languages, such as Java and C#, you don’t have to explicitly allocate or de-allocate memory in your code; it’s all managed for you in the background.

Question:- How do we initialise the complex types with GO-lang ?

Answer:- There are two built-in functions here, that we should be aware of :

  • 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.

Question:- Can you demonstrate the usage of new() built-in functions ?

Answer:- Let’s take a look at this bit of code :-

  • 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.

Question:- Can you demonstrate the usage of make() built-in function ?

Answer:- Let’s take a look at this bit of code :-

  • 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.

Question:- Can you demonstrate the usage of arrays with help of make() built-in function ?

Answer:- Let’s take a look at this bit of code :-

  • 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.

Question:- How is memory deallocated with Go-Lang ?

Answer:- Memory is de-allocated automatically by the garbage collector that’s built into the Go runtime.

  • 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.

Question:- Explain the automatic memory management with GoLang ?

Answer:- Go has a garbage collector, which means you don’t have to deal with memory management. When you allocate an object and then stop using it, Go’s garbage collector will clear it up.

Question:- Whats the concept of Resource in GoLang ?

Answer:- Go have various kind of resources. For example :- Memory, Files, Sockets, Virtual-Machines and others. You’d like to make sure that these resources are closed when you’re done with them as well.

Question:- How do we free-up resources in GoLang ?

Answer:- To make sure a resource is closed, we basically use defer. Let’s see an example :-

  • 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.

Question:- Demonstrate an example of invoking external URL along with usage of defer, in GoLang ?

Answer:- Below method demonstrates that, a particular function can very well return multiple data from one single method :-

  • 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.

Question:- Demonstrate the usage of error in GoLang ?

Answer:- Go functions can return more than one value. This is used extensively in Go to signal errors. Error is a built-in type that’s used throughout Go. The function that can error, will usually return the error value as the last value returned.

  • 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.

You are going to write a lot of : if err does not equal nil code. You might find this tedious, but I personally found it made my code much more robust, and forced me to think about every error.

Question:- Does GoLang also have PANIC and Should we use that ?

Answer:- Yes, Go also has something called panic, which is somewhat similar to exceptions in other languages. The use of panic is discouraged in Go and is considered an anti-pattern. The way to signal an error is to return it.

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next part of this series with Hands-On with Redis-Cluster.

References :-

--

--