loading...
Cover image for Day 7 of #30DaysOfCode | lifetime of variables in golang

Day 7 of #30DaysOfCode | lifetime of variables in golang

shubhamsaurav profile image Shubham Saurav ・2 min read

Today is the 7th day of my #30DaysOfCode challenge. I have so many exciting things to share with you guys. So let's just get into it.

The first thing that I would like to share is about the lifetime of the variables. Some of you know that Golang is a garbage-collected language. Being a garbage collected language means that the memory will be automatically reclaimed by the language itself. The developers don't have to worry about collecting or reclaiming the memory in code manually. The language will do that for you. But this doesn't mean that developers don't need to think about memory or learn about the lifetime of variables. To write efficient programs, developers still need to be aware of the lifetime of variables. So, let's learn about that.

What is the lifetime of a variable?
The lifetime of the variable is the time interval in which the variable exists as the program runs.

The lifetime of variables in Golang:
The package-level variable exists during the entire execution of the program while the local variable exists for a limited period of time. A local variable is created each time the declaration is executed and it lives on until it becomes unreachable and then, their memory is reclaimed. The function parameters and loop iterator variables are examples of local variables. The function variables are created each time their function is called. The loop iterator variables are created each time the loop begins. And when the variables become unreachable, the Go compiler reclaims their memory.

Okay. That was a brief introduction to the lifetime of variables in Go. Now, let's talk about a pet-practice project I am working on.

Some of you may know that I am also learning to write Restful APIs using NodeJs. And today, I have started working on a Journal API. At the moment, this project is just for fun and practice. Nothing serious but the project will be full packed though. The API will have all the functionality that any production project has. And I will be making it open source in the end.

Alright! That's enough talk for today. And hopefully, I will see you guys tomorrow.

Connect With Me:
Youtube: ShubhamSauravYT.
Twitter: @hiShubhamSaurav
Instagram: @hiShubhamSaurav
Facebook: @hiShubhamSaurav

Posted on by:

shubhamsaurav profile

Shubham Saurav

@shubhamsaurav

Writing Restful APIs using JS. Learning to code in Go(Golang)

Discussion

markdown guide