The Go Programming Language
Rate it:
Open Preview
Read between February 26 - October 30, 2018
2%
Flag icon
gopl.io.
17%
Flag icon
On the other hand, if we really care about the individual Unicode characters, we have to use other mechanisms. Consider the string from our very first example, which includes two East Asian characters. Figure 3.5 illustrates its representation in memory. The string contains 13 bytes, but interpreted as UTF-8, it encodes only nine code points or runes: Figure 3.5. A range loop decodes a UTF-8-encoded string. Click here to view code image import "unicode/utf8" s := "Hello, " fmt.Println(len(s))                    // "13" fmt.Println(utf8.RuneCountInString(s)) // "9" To process those characters, ...more
17%
Flag icon
But this is clumsy, and we need loops of this kind all the time. Fortunately, Go’s range loop, when applied to a string, performs UTF-8 decoding implicitly. The output of the loop below is also shown in Figure 3.5; notice how the index jumps by more than 1 for each non-ASCII rune.
18%
Flag icon
Four standard packages are particularly important for manipulating strings: bytes, strings, strconv, and unicode.
22%
Flag icon
The ellipsis “...” in the declaration of appendInt makes the function variadic: it accepts any number of final arguments.
38%
Flag icon
but what makes Go’s interfaces so distinctive is that they are satisfied implicitly.
41%
Flag icon
(One of the authors apologizes for the other author’s musical tastes.)
Mohamed Elsherif
LOL
Mosab
· Flag
Mosab
Caught my eyes too! :D
53%
Flag icon
The Tick function is convenient, but it’s appropriate only when the ticks will be needed throughout the lifetime of the application. Otherwise, we should use this pattern: Click here to view code image ticker := time.NewTicker(1 * time.Second) <-ticker.C // receive from the ticker's channel ticker.Stop() // cause the ticker's goroutine to terminate
54%
Flag icon
There’s a handy trick we can use during testing: if instead of returning from main in the event of cancellation, we execute a call to panic, then the runtime will dump the stack of every goroutine in the program. If the main goroutine is the only one left, then it has cleaned up after itself.
56%
Flag icon
good rule of thumb is that there is no such thing as a benign data race.
56%
Flag icon
A data race occurs whenever two goroutines access the same variable concurrently and at least one of the accesses is a write. It follows from this definition that there are three ways to avoid a data race.
56%
Flag icon
This is what is meant by the Go mantra “Do not communicate by sharing memory; instead, share memory by communicating.”
56%
Flag icon
If each stage of the pipeline refrains from accessing the variable after sending it to the next stage, then all accesses to the variable are sequential. In effect, the variable is confined to one stage of the pipeline, then confined to the next, and so on. This discipline is sometimes called serial confinement.
56%
Flag icon
The third way to avoid a data race is to allow many goroutines to access the variable, but only one at a time. This approach is known as mutual exclusion
57%
Flag icon
A defer is marginally more expensive than an explicit call to Unlock, but not enough to justify less clear code. As always with concurrent programs, favor clarity and resist premature optimization. Where possible, use defer and let critical sections extend to the end of a function.
57%
Flag icon
All these concurrency problems can be avoided by the consistent use of simple, established patterns. Where possible, confine variables to a single goroutine; for all other variables, use mutual exclusion.
57%
Flag icon
sync.Once
58%
Flag icon
The Race Detector
67%
Flag icon
For example, the implementation of the fmt package needs the functionality of unicode.IsSpace as part of fmt.Scanf. To avoid creating an undesirable dependency, fmt does not import the unicode package and its large tables of data; instead, it contains a simpler implementation, which it calls isSpace. To ensure that the behaviors of fmt.isSpace and unicode.IsSpace do not drift apart, fmt prudently contains a test. It is an external test, and thus it cannot access isSpace directly, so fmt opens a back door to it by declaring an exported variable that holds the internal isSpace function. This is ...more
67%
Flag icon
premature abstraction:
67%
Flag icon
By its nature, testing is never complete. As the influential computer scientist Edsger Dijkstra put it, “Testing shows the presence, not the absence of bugs.” No quantity of tests can ever prove a package free of bugs. At best, they increase our confidence that the package works well in a wide range of important scenarios.
69%
Flag icon
The final two chapters of the book examine the reflect and unsafe packages, which few Go programmers regularly use—and even fewer need to use. If you haven’t written any substantial Go programs yet, now would be a good time to do that.
77%
Flag icon
For now, put the last two chapters in the back of your mind. Write some substantial Go programs. Avoid reflect and unsafe; come back to these chapters only if you must.