Home >Backend Development >Golang >A brief discussion on memory allocation from the perspective of Go language

A brief discussion on memory allocation from the perspective of Go language

Go语言进阶学习
Go语言进阶学习forward
2023-07-24 15:45:17525browse

#Why you should learn C language

Well,,, this year is 2023. If we look at various current development projects, C language is indeed rarely used. Except for some certain fields such as embedded, it is rarely seen. There is no trace of C, but why do I still hear xx saying that I need to learn C language well?

According to my understanding, in fact, when learning C language, you are not learning this language, but It is the things behind this language, the designs it represents, and the essence it represents.


What advantages will it bring if you learn C language well?

I believe that many newbies may have heard a certain boss say in the group, I finished learning xx language in one week, and then started working directly. Then you said silently, pretend to be 13, or maybe you are in the group There was a direct fight.

So, can it really be said that one can learn a language in one week or two weeks?

I can be responsible and tell you that there is absolutely no problem, but it needs to be built on a strong foundation.

Pointers are a particularly important foundation. No matter which language is used nowadays, the essence is the application of pointers, but the encapsulation is simple.

But what this causes is that if the foundation is unstable, it will cause some inexplicable and incomprehensible problems, especially for newbies.

So, no matter at any stage now, those who have not learned C language must learn C and pick up the important foundation that has been left behind.

So, let me give you a brief introduction first.


Required technology stack

Technology stack used this time It is Go language. In theory, it uses C language. However, when getting started with pointers, the essence of Go and C are basically the same, so the more user-friendly Go language is used.

We will add Python later and compare it with Go to see what the difference is and see how the popular Python allocates memory.

Currently, when reading this article, I still recommend that friends have a certain foundation, at least understand how to define variables and so on.


##The relationship between the hardware of the operating system

First, we need to understand Let’s look at the main components of a computer.

First of all, simply speaking, at least by CPU Memory stick Hard disk Graphics card Monitor Operating system (Linux, Windows)wait.

So how do these hardware cooperate with each other?

A brief discussion on memory allocation from the perspective of Go language

Well...that's probably what it looks like, a bit ugly.



In short:

  1. Every application on the system (qq, WeChat, etc.) that wants to let the computer play music, display something on the monitor, or write a file on the hard disk, all needs the consent of the operating system.

  2. The operating system calls the CPU to complete the execution of instructions, and the CPU reads the open application into the memory stick. Therefore, why is the mechanical hard disk so stuck? The machine is too slow, and now the solid state can complete the transfer of hard disk data to memory stick data in a swish.

  3. The CPU analyzes the application data read into the memory stick. If it is data processed by the CPU, take it and calculate it yourself. If it is data processed by the graphics card, call Calculate it for the graphics card, if it is sound data, calculate it for the sound card, etc.

  4. Through the above coordination, the entire application can basically run. If data needs to be written to the hard disk, the CPU can just write the data in the memory stick to the hard disk. .

Okay, through the above, we can basically understand the coordination relationship between the operating system and each hardware.

Let me tell you a secret, the code we write is basically running in the memory stick. In the same way, the above-mentioned application is basically the code that will be applied. Running in the memory stick.


Memory stick

Yes, that’s it, a memory stick.

A brief discussion on memory allocation from the perspective of Go language

Similarly, I can also understand the internal structure of the memory stick in this way.

We can understand the memory sticks as houses, just like this.

A brief discussion on memory allocation from the perspective of Go language

It’s still ugly...

But our memory stick has The coordinates of are also called address, placed in each small grid with each value.

A brief discussion on memory allocation from the perspective of Go language

But usually, we understand it flatly, like this.

#Addresses (coordinates) are generally expressed in hexadecimal, and whatever the value should be is stupid.

A brief discussion on memory allocation from the perspective of Go language

Code Brief

Let’s look at a very short Go code.

Declare an age variable and assign it a value of 18, and output it. How

package main


import "fmt"


func main() {
    var age int = 18
    fmt.Println("我今年:",age,"岁")
}

is represented in memory.

A brief discussion on memory allocation from the perspective of Go language

Our variable name age points to 0x1001 address, and the value stored in 0x0001 is output.

like this.

A brief discussion on memory allocation from the perspective of Go language

Note: Be sure to understand this sentence, the variable name points to the memory module the address of.

The above age points to the address 0x1001.

But we are printing At this time, the we print is the value in the address . Similarly, when we assign values ​​and exchange variables, is still the

# in the address

##value. For example:

A brief discussion on memory allocation from the perspective of Go language

A brief discussion on memory allocation from the perspective of Go language

#Memory allocation diagram of the above code.

age2 will re-apply for an address to store the value of age 18.

Sometimes someone may ask, on Saturday, if there is such a situation. They both point to the same memory at the same time. Wouldn't this save more memory?

A brief discussion on memory allocation from the perspective of Go language

Result: Of course, we will talk about this situation later. Just understand it now, step by step.


Conclusion

The variable name points to Is the memory address .

When we assign a value, or when we operate variables such as printing, we operate on the value above the address .


End

The above is already very long and you may be a little dizzy. , this article ends here.

If you have any task problems during the operation, please remember to leave a message below and we will solve the problem as soon as possible.

I am a coder on Zhouba. If you think it is good, please remember to give it a like.

Thank you for watching.

If you have any problems during the operation, please leave a message below and we will solve the problem as soon as possible.

Use a smile to tell others that I am better today than yesterday, and I will be the same in the future.

If you think the article is good, remember to like it Leave a message to support us . Thank you for reading. If you have any questions, please leave a message below~

If you want to learn more about Python, you can refer to the learning website: http://pdcfighting.com/, click Read the original text and you can go directly~##

-------- -------- ---## End ##------ -------------

# Recommended articles from past issues: A brief discussion on memory allocation from the perspective of Go language

A brief discussion on memory allocation from the perspective of Go language

welcome everybody Like, leave a message, forward, reprint, Thank you all for your company and support

If you want to join the Go learning group, please reply in the background [ Join the group]

Thousands of rivers and mountains are always love, click [Looking】Is that okay?

##

The above is the detailed content of A brief discussion on memory allocation from the perspective of Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete