LEARN GOLANG| Part-3

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.

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.

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.

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.

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.

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.

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.

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.

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 :-

--

--

Software Engineer for Big Data distributed systems

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