Skip to content

Commit

Permalink
fix a bug in deleting sublsice example
Browse files Browse the repository at this point in the history
  • Loading branch information
go101 committed Oct 30, 2019
1 parent 8b366bd commit 35b8f5e
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 29 deletions.
5 changes: 5 additions & 0 deletions UPDATES.md
@@ -1,3 +1,8 @@
### 1.13.g (2019/Oct/30)

* fix a bug in the example code in the [Delete a segment of slice elements](https://gfw.go101.org/article/container.html#delete-slice-elements) section
of the "Arrays, Slices and Maps in Go" article.

### 1.13.f (2019/Oct/18)

* correct the explanations for the [Evaluation and Assignment Orders in Assignment Statements](https://go101.org/article/evaluation-orders.html#value-assignment) section
Expand Down
1 change: 1 addition & 0 deletions articles/acknowledgements.html
Expand Up @@ -137,6 +137,7 @@ <h1>Acknowledgements</h1>
Amargin,
Tony Bai,
lanastasov,
liukaifei,
etc.
</p>

Expand Down
24 changes: 14 additions & 10 deletions articles/blocks-and-scopes.html
Expand Up @@ -87,7 +87,7 @@ <h3>Code Blocks</h3>
</ul>

<p><i>
(The differences to Go specification are to make the explanation of identifier shadowing simpler below.)
(The differences to Go specification are to make the below explanations for identifier shadowing simpler.)
</i></p>

Here is a picture shows the block hierarchies in a program:
Expand Down Expand Up @@ -217,25 +217,29 @@ <h3>Source Code Element Declaration Places</h3>
Please note,
<ul>
<li>
ignore labels, two code elements can be declared with the same identifier as long as
the respective innermost containing code blocks of the two declarations are different.
if the innermost containing blocks of two code element declarations are the same one,
then the names (identifiers) of the two code elements can't be identical.
</li>
<li>
if the innermost containing function body blocks of the two label declarations are the same one,
then the names (identifiers) of two labels can't be identical.
the name (identifier) of a package-level code element declared in a package must not
be identical to any package import name declared in any source file in the package.
</li>
<li>
the references of a label must be within the innermost function body block containing the declaration of the label.
if the innermost containing function body blocks of two label declarations are the same one,
then the names (identifiers) of the two labels can't be identical.
</li>
<li>
some special portions in the implicit local blocks in all kinds of control flows have special requirements.
Generally, no code elements are allowed to be directly declared in such implicit local blocks, except some short variable declarations.
the references of a label must be within the innermost function body block containing the declaration of the label.
</li>
<li>
some special portions in the implicit local blocks in all kinds of control flows have special requirements.
Generally, no code elements are allowed to be directly declared in such implicit local blocks, except some short variable declarations.
<ul>
<li>
Each <code>if</code>, <code>switch</code> or <code>for</code> keyword can be closely followed by a short variable declaration.
Each <code>if</code>, <code>switch</code> or <code>for</code> keyword can be closely followed by a short variable declaration.
</li>
<li>
Each <code>case</code> keyword in a <code>select</code> control flow can be closely followed by a short variable declaration.
Each <code>case</code> keyword in a <code>select</code> control flow can be closely followed by a short variable declaration.
</li>
</ul>
</li>
Expand Down
18 changes: 14 additions & 4 deletions articles/container.html
Expand Up @@ -2198,8 +2198,12 @@ <h4>Delete a segment of slice elements</h4>
s = s[:from + copy(s[from:], s[to:])]

// Don't preserve element orders:
copy(s[from:to], s[len(s)+from-to:])
s = s[:len(s)+from-to]
if n := to-from; len(s)-to < n {
copy(s[from:to], s[to:])
} else {
copy(s[from:to], s[len(s)-n:])
}
s = s[:len(s)-(to-from)]
</code></pre>

If the slice elements reference other values, we should reset
Expand Down Expand Up @@ -2316,6 +2320,14 @@ <h4>Insert all elements of a slice into another slice</h4>
s = append(elements, s...)
</code></pre>

<p>
The <code>make</code> call in the above code snippet clear the memory allocated for for slice <code>x</code>,
this is actually an unnecessary operation for this specified use case.
<a href="https://github.com/golang/go/issues/31592">Future compiler optimization</a> might remove the clear operation.
</p>

</div>

<h4>Insert several individual elements</h4>

<p>
Expand Down Expand Up @@ -2356,8 +2368,6 @@ <h4>More slice operations</h4>
introduced above in the built-in way.
</p>

</div>

<h3>Use Maps to Simulate Sets</h3>

<p>
Expand Down
2 changes: 1 addition & 1 deletion articles/defer-more.html
Expand Up @@ -176,7 +176,7 @@ <h3>Performance Losses Caused by Deferring Function Calls</h3>
For the official Go compiler, before version 1.13,
deferred function calls will cause a few performance losses at run time.
Since Go SDK 1.13, some simple defer use cases have got optimized.
Go SDK 1.14 will optimize deferfed calls more.
Go SDK 1.14 will optimize deferred calls more.
</p>

<!--
Expand Down
3 changes: 3 additions & 0 deletions articles/details.html
Expand Up @@ -2186,6 +2186,9 @@ <h3>Since Go 1.6, we can .</h3>
https://github.com/golang/go/issues/34684
syscall.Proc.Call and syscall.LazyProc.Call are not implemented in assembly, and, as such, this rule does not apply to them.
The print result of nil and blank slices are the same. (Similar to maps).
* history reason
-->


Expand Down
4 changes: 2 additions & 2 deletions articles/function-declarations-and-calls.html
Expand Up @@ -244,8 +244,8 @@ <h3>Function Calls</h3>
</div>

<p>
Function calls can be deferred and invoked in new goroutines (green threads) in Go.
Please read the article <a href="control-flows-more.html">deferred function calls and goroutines</a> for details.
Function calls can be deferred or invoked in new goroutines (green threads) in Go.
Please read <a href="control-flows-more.html">a later article</a> for details.
</p>

<a class="anchor" id="existing-phase"></a>
Expand Down
2 changes: 1 addition & 1 deletion articles/function.html
Expand Up @@ -425,7 +425,7 @@ <h4>Use function calls as expressions</h4>
</p>

If the return results of a call to a multi-result function are not discarded,
then the call only can be used as a multi-value expression in two scenarios.
then the call can only be used as a multi-value expression in two scenarios.
<ol>
<li>
The call can be used in an assignment as source values.
Expand Down
6 changes: 6 additions & 0 deletions articles/quizzes.html
Expand Up @@ -5,6 +5,12 @@
Please follow Go 101's <a href="https://twitter.com/go100and1">official twitter account</a> to get the newest quizzes.):

<ul>
<li>
<a href="https://twitter.com/go100and1/status/1189158330930843648" target="_blank">What does the following Go program print? (21)</a>
</li>
<li>
<a href="https://twitter.com/go100and1/status/1188841103572623360" target="_blank">What does the following Go program print? (20)</a>
</li>
<li>
<a href="https://twitter.com/go100and1/status/1187055967168450560" target="_blank">What does the following Go program print? (19)</a>
</li>
Expand Down
6 changes: 5 additions & 1 deletion articles/struct.html
Expand Up @@ -163,8 +163,10 @@ <h3>Struct Value Literals and Struct Value Manipulations</h3>

<p>
For a value <code>v</code> of type <code>S</code>, we can use
<code>v.x</code> and <code>v.y</code>, which are called selectors,
<code>v.x</code> and <code>v.y</code>, which are called selectors (or selector expressions),
to represent the field values of <code>v</code>.
<code>v</code> is called the receiver of the selectors.

Later, we call the dot <code>.</code> in a selector as the property selection operator.
</p>

Expand Down Expand Up @@ -343,6 +345,8 @@ <h3>In Selectors, Struct Pointers Can Be Used as Struct Values</h3>
// Use struct pointers as structs.
book2.pages = book1.pages
// This last line is eqivalent to the next line.
// In other words, if the receiver is a pointer,
// it will be automatic dereferenced.
(*book2).pages = (*book1).pages
}
</code></pre>
Expand Down
20 changes: 10 additions & 10 deletions articles/tips.html
Expand Up @@ -784,14 +784,6 @@ <h3>Optimize Go code by making use of BCE (bounds check elimination).</h3>
select may slow: https://twitter.com/melvinodsa/status/1031434710184939522
<a class="anchor" id="embed-unexported-alias-of-exported-type"></a>
<h3>Embedding an unexported alias of an exported type to avoiding the corresponding anonymous field being modified in user packages.</h3>
type t = T
type X struct {
t
}
try to share Transport for http.Client values. (need a go practice article)
avoid using the http.Get, ..., functions directly.
Expand All @@ -812,7 +804,7 @@ <h3>Embedding an unexported alias of an exported type to avoiding the correspond
+setting GOMAXPROCS with a value larger than NumCPUs is good for the performance of a program sometimes^M
+https://news.ycombinator.com/item?id=18350362^
simulate subPackages
simulate subPackages (not work for exported types)
package foo / import "a.b/foo"
type SubPkg_ struct{}
var SubPkg SubPkg_
Expand All @@ -828,8 +820,16 @@ <h3>Embedding an unexported alias of an exported type to avoiding the correspond
req.WithContext(context.WithValue(req.Context(), paramsKeyType{}, Params{path, tokens})) is slow
https://github.com/golang/go/issues/28737
<a class="anchor" id="embed-unexported-alias-of-exported-type"></a>
<h3>Embedding an unexported alias of an exported type to avoiding the corresponding anonymous field being modified in user packages.</h3>
type t = T
type X struct {
t
}
how to embed an exported type as a non-export field?
so that package users can modify the field.
so that package users can't modify the field.
type embeddedType = ExportedType
use make() instead of composite literals to initilize map and slice
Expand Down
4 changes: 4 additions & 0 deletions articles/unofficial-faq.html
Expand Up @@ -1447,6 +1447,10 @@ <h3>
* wg align
* atomic order guarantee
What are the differences between type definition declarations and type alias declarations?
* direct method set not obtained for definition
* defined pointer type whose base is interface and pointer can't be embedded
* method receiver with alias will decalre the method on the rrepresented type
-->

0 comments on commit 35b8f5e

Please sign in to comment.