-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Go: Memory Model entry file #1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d58516b
Add Go Memory Model entry file
ethersea 13ee6d7
Merge branch 'main' into go-memory-entry
SSwiniarski 804999d
Update content/go/concepts/memory/go-memory.md
ethersea 051f2db
Fix tests
yangc95 fd77574
Reword beginning of paragraph
yangc95 05adc4b
Merge branch 'main' into go-memory-entry
SSwiniarski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| --- | ||
| Title: 'Memory' | ||
| Description: 'The Go memory model specifies the rules for how memory is accessed and modified in concurrent programs.' | ||
| Subjects: | ||
| - 'Computer Science' | ||
| - 'Developer Tools' | ||
| Tags: | ||
| - 'Concurrency' | ||
| - 'Data Structures' | ||
| - 'Memory' | ||
| - 'Variables' | ||
| CatalogContent: | ||
| - 'learn-go' | ||
| - 'paths/back-end-engineer-career-path' | ||
| - 'paths/computer-science' | ||
| --- | ||
|
|
||
| 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. | ||
|
|
||
| ## Memory Operations | ||
|
|
||
| In Go, the concurrent execution of multiple threads includes 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: | ||
|
|
||
| ```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 in the program. In Go, memory is automatically managed using a garbage collector, so explicit freeing of memory is not usually necessary. | ||
|
|
||
| ## Concurrent Access to Shared Memory | ||
|
|
||
| One important aspect of the Go memory model is that it allows multiple threads to access shared memory concurrently. This feature allows Go programs to take advantage of multiple CPU cores and improve performance. Access to the shared data within memory should be serialized. | ||
|
|
||
| ## 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 which depends on the written value. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.