Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions content/go/concepts/memory/go-memory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
Title: 'Go: Memory'
Description: 'The Go memory model specifies the rules for how memory is accessed and modified in concurrent programs. It allows for the concurrent execution of multiple threads and includes operations such as reading, writing, allocating, and freeing memory. '
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description could be shorter. I'd remove the second sentence and incorporate the information in the first paragraph of the article proper.

Subjects:
- 'Computer Science'
- 'Developer Tools'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the extra line breaks between sections in the metadata.

Tags:
- 'Concurrency'
- 'Data Structures'
- 'Memory'
- 'Variables'

CatalogContent:
- 'learn-go'
- 'paths/back-end-engineer-career-path'
- 'paths/computer-science'
---

# Go: Memory Model
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this header. The title in the metadata will be the title when the article is published. H1 headers should not be used in the article body.


The Go programming language has a specific memory model that describes how variables are stored in memory and how they can be accessed by different threads. This memory
model is important because it determines the behavior of programs that use concurrency, or the execution of multiple threads at the same time.

## Memory Operations

In Go, there are several basic operations that can be performed on memory:

1. **Reading**: This operation involves accessing a memory location and obtaining the value stored at that location. For example:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be laid out better if headers were used instead of the list you're using now. i.e.

### 1. Reading

This operation involves accessing a memory location and obtaining the value stored at that location...

### 2. Writing

This operation involves storing a value in a specific memory location...

etc.


```go
x := 10
y := x
```

In this code, the value of `x` is being read and stored in the variable `y`.

2. **Writing**: This operation involves storing a value in a specific memory location. For example:

```go
x := 10
y := 20
x = y
```
In this code, the value of `y` is being written to the memory location associated with `x`.

3. **Allocating**: This operation involves creating a new block of memory and returning a pointer to the start of the block. In Go, this can be done using the `new`
function:

```go
x := new(int)
```

This code creates a new block of memory for an `int` value and stores a pointer to the start of the block in the variable `x`.

4. **Freeing**: This operation involves releasing a block of memory that is no longer needed, so that it can be reused by the program. In Go, memory is automatically
managed using a garbage collector, so explicit freeing of memory is not usually necessary.

## Memory Model

The Go memory model is a set of rules that dictate how memory operations can be performed in a concurrent program. These rules specify the order in which memory operations must be performed and how they can be observed by different threads.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we go into a little more details about what the rules are?


### Concurrent Access to Shared Memory

One important aspect of the Go memory model is that it allows multiple threads to access shared memory concurrently, as long as they follow the rules for memory operations. This feature allows Go programs to take advantage of multiple CPU cores and improve performance.

### Ordering of Memory Operations

However, the Go memory model also requires that certain memory operations be performed in a specific order to ensure that the program behaves correctly. For example, a write operation must occur before a read operation that depends on the written value.

### Importance of the Go Memory Model

Overall, the Go memory model is a crucial aspect of the language that enables efficient and correct concurrent programming. Understanding how it works is essential for writing correct and performant Go programs.