mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 00:47:11 +08:00
Images.
This commit is contained in:
parent
66b1d95047
commit
7274bf3b69
13 changed files with 27 additions and 2 deletions
|
|
@ -7,6 +7,8 @@ hide:
|
|||
|
||||
# Interface pollution
|
||||
|
||||

|
||||
|
||||
Interfaces are one of the cornerstones of the Go language when designing and structuring our code. However, like many tools or concepts, abusing them is generally not a good idea. Interface pollution is about overwhelming our code with unnecessary abstractions, making it harder to understand. It’s a common mistake made by developers coming from another language with different habits. Before delving into the topic, let’s refresh our minds about Go’s interfaces. Then, we will see when it’s appropriate to use interfaces and when it may be considered pollution.
|
||||
|
||||
## Concepts
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ hide:
|
|||
|
||||
# Writing concurrent code that leads to false sharing
|
||||
|
||||

|
||||
|
||||
In previous sections, we have discussed the fundamental concepts of CPU caching. We have seen that some specific caches (typically, L1 and L2) aren’t shared among all the logical cores but are specific to a physical core. This specificity has some concrete impacts such as concurrency and the concept of false sharing, which can lead to a significant performance decrease. Let’s look at what false sharing is via an example and then see how to prevent it.
|
||||
|
||||
In this example, we use two structs, `Input` and `Result`:
|
||||
|
|
|
|||
BIN
docs/img/false-sharing.jpeg
Normal file
BIN
docs/img/false-sharing.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 150 KiB |
BIN
docs/img/interface-pollution.jpeg
Normal file
BIN
docs/img/interface-pollution.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 127 KiB |
|
|
@ -1,6 +1,8 @@
|
|||
---
|
||||
title: Japanese Version
|
||||
comments: true
|
||||
hide:
|
||||
- navigation
|
||||
---
|
||||
|
||||
# Go言語でありがちな間違い
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
---
|
||||
title: Chinese (Simplified) Version
|
||||
comments: true
|
||||
hide:
|
||||
- navigation
|
||||
---
|
||||
|
||||
# 100个Go常见错误及如何避免
|
||||
|
||||

|
||||
|
||||
## 代码及工程组织
|
||||
|
||||
### 意外的变量隐藏 (#1)
|
||||
|
|
|
|||
|
|
@ -1037,6 +1037,7 @@
|
|||
|
||||
|
||||
<h1 id="interface-pollution">Interface pollution</h1>
|
||||
<p><a class="glightbox" href="../img/interface-pollution.jpeg" data-type="image" data-width="auto" data-height="auto" data-desc-position="bottom"><img alt="" src="../img/interface-pollution.jpeg" /></a></p>
|
||||
<p>Interfaces are one of the cornerstones of the Go language when designing and structuring our code. However, like many tools or concepts, abusing them is generally not a good idea. Interface pollution is about overwhelming our code with unnecessary abstractions, making it harder to understand. It’s a common mistake made by developers coming from another language with different habits. Before delving into the topic, let’s refresh our minds about Go’s interfaces. Then, we will see when it’s appropriate to use interfaces and when it may be considered pollution.</p>
|
||||
<h2 id="concepts">Concepts</h2>
|
||||
<p>An interface provides a way to specify the behavior of an object. We use interfaces to create common abstractions that multiple objects can implement. What makes Go interfaces so different is that they are satisfied implicitly. There is no explicit keyword like <code>implements</code> to mark that an object X implements interface Y.</p>
|
||||
|
|
|
|||
|
|
@ -879,6 +879,7 @@
|
|||
|
||||
|
||||
<h1 id="writing-concurrent-code-that-leads-to-false-sharing">Writing concurrent code that leads to false sharing</h1>
|
||||
<p><a class="glightbox" href="../img/false-sharing.jpeg" data-type="image" data-width="auto" data-height="auto" data-desc-position="bottom"><img alt="" src="../img/false-sharing.jpeg" /></a></p>
|
||||
<p>In previous sections, we have discussed the fundamental concepts of CPU caching. We have seen that some specific caches (typically, L1 and L2) aren’t shared among all the logical cores but are specific to a physical core. This specificity has some concrete impacts such as concurrency and the concept of false sharing, which can lead to a significant performance decrease. Let’s look at what false sharing is via an example and then see how to prevent it.</p>
|
||||
<p>In this example, we use two structs, <code>Input</code> and <code>Result</code>:</p>
|
||||
<div class="language-go highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">type</span><span class="w"> </span><span class="nx">Input</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span>
|
||||
|
|
|
|||
BIN
site/img/false-sharing.jpeg
Normal file
BIN
site/img/false-sharing.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 150 KiB |
BIN
site/img/interface-pollution.jpeg
Normal file
BIN
site/img/interface-pollution.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 127 KiB |
|
|
@ -363,7 +363,9 @@
|
|||
|
||||
|
||||
|
||||
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
|
||||
|
||||
|
||||
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" hidden>
|
||||
<div class="md-sidebar__scrollwrap">
|
||||
<div class="md-sidebar__inner">
|
||||
|
||||
|
|
@ -824,6 +826,8 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
<div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
|
||||
<div class="md-sidebar__scrollwrap">
|
||||
<div class="md-sidebar__inner">
|
||||
|
|
@ -3564,6 +3568,8 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h2 id="__comments">Comments</h2>
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -363,7 +363,9 @@
|
|||
|
||||
|
||||
|
||||
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
|
||||
|
||||
|
||||
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" hidden>
|
||||
<div class="md-sidebar__scrollwrap">
|
||||
<div class="md-sidebar__inner">
|
||||
|
||||
|
|
@ -824,6 +826,8 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
<div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
|
||||
<div class="md-sidebar__scrollwrap">
|
||||
<div class="md-sidebar__inner">
|
||||
|
|
@ -1935,6 +1939,7 @@
|
|||
|
||||
|
||||
<h1 id="100go">100个Go常见错误及如何避免</h1>
|
||||
<p><a class="glightbox" href="../img/inside-cover.png" data-type="image" data-width="auto" data-height="auto" data-desc-position="bottom"><img alt="" src="../img/inside-cover.png" /></a></p>
|
||||
<h2 id="_1">代码及工程组织</h2>
|
||||
<h3 id="1">意外的变量隐藏 (#1)</h3>
|
||||
<p>避免变量隐藏(外部作用域变量被内部作用域同名变量隐藏),有助于避免变量引用错误,有助于他人阅读理解。</p>
|
||||
|
|
@ -2284,6 +2289,8 @@
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h2 id="__comments">Comments</h2>
|
||||
|
|
|
|||
Loading…
Reference in a new issue