1
0
mirror of https://github.com/golang/go synced 2024-11-21 15:24:45 -07:00

FAQ: many small fixes and adjustments

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5685048
This commit is contained in:
Rob Pike 2012-02-20 12:42:50 +11:00
parent 05e80cffc3
commit 5cff1903ea

View File

@ -485,6 +485,7 @@ or how the <code>image</code> packages generate compressed
image files. All these ideas stem from a single interface image files. All these ideas stem from a single interface
(<code>io.Writer</code>) representing a single method (<code>io.Writer</code>) representing a single method
(<code>Write</code>). And that's only scratching the surface. (<code>Write</code>). And that's only scratching the surface.
Go's interfaces have a profound influence on how programs are structured.
</p> </p>
<p> <p>
@ -840,12 +841,12 @@ there are multiple considerations involving shallow vs. deep comparison, pointer
value comparison, how to deal with recursive types, and so on. value comparison, how to deal with recursive types, and so on.
We may revisit this issue&mdash;and implementing equality for slices We may revisit this issue&mdash;and implementing equality for slices
will not invalidate any existing programs&mdash;but without a clear idea of what will not invalidate any existing programs&mdash;but without a clear idea of what
equality of structs and arrays should mean, it was simpler to leave it out for now. equality of slices should mean, it was simpler to leave it out for now.
</p> </p>
<p> <p>
In Go 1, equality is defined for structs and arrays, so such In Go 1, unlike prior releases, equality is defined for structs and arrays, so such
types can be used as map keys, but slices still do not have a definition of equality. types can be used as map keys. Slices still do not have a definition of equality, though.
</p> </p>
<h3 id="references"> <h3 id="references">
@ -941,7 +942,7 @@ func (s MyStruct) valueMethod() { } // method on value
For programmers unaccustomed to pointers, the distinction between these For programmers unaccustomed to pointers, the distinction between these
two examples can be confusing, but the situation is actually very simple. two examples can be confusing, but the situation is actually very simple.
When defining a method on a type, the receiver (<code>s</code> in the above When defining a method on a type, the receiver (<code>s</code> in the above
example) behaves exactly as if it were an argument to the method. examples) behaves exactly as if it were an argument to the method.
Whether to define the receiver as a value or as a pointer is the same Whether to define the receiver as a value or as a pointer is the same
question, then, as whether a function argument should be a value or question, then, as whether a function argument should be a value or
a pointer. a pointer.
@ -1082,15 +1083,15 @@ See the <a href="/doc/codewalk/sharemem/">Share Memory By Communicating</a> code
Why doesn't my multi-goroutine program use multiple CPUs?</h3> Why doesn't my multi-goroutine program use multiple CPUs?</h3>
<p> <p>
You must set <code>GOMAXPROCS</code> to allow the You must set the <code>GOMAXPROCS</code> shell environment variable
or use the similarly-named <a href="/pkg/runtime/#GOMAXPROCS"><code>function</code></a>
of the runtime package to allow the
run-time support to utilize more than one OS thread. run-time support to utilize more than one OS thread.
</p> </p>
<p> <p>
Programs that perform parallel computation should benefit from an increase in Programs that perform parallel computation should benefit from an increase in
<code>GOMAXPROCS</code>. (See the <a <code>GOMAXPROCS</code>.
href="http://golang.org/pkg/runtime/#GOMAXPROCS"><code>runtime</code> package's
documentation</a>.)
</p> </p>
<h3 id="Why_GOMAXPROCS"> <h3 id="Why_GOMAXPROCS">
@ -1148,7 +1149,10 @@ there is no useful way for a method call to obtain a pointer.
</p> </p>
<p> <p>
If not for this restriction, this code: Even in cases where the compiler could take the address of a value
to pass to the method, if the method modifies the value the changes
will be lost in the caller.
As a common example, this code:
</p> </p>
<pre> <pre>
@ -1174,7 +1178,7 @@ Consider the following program:
func main() { func main() {
done := make(chan bool) done := make(chan bool)
values := []string{ "a", "b", "c" } values := []string{"a", "b", "c"}
for _, v := range values { for _, v := range values {
go func() { go func() {
fmt.Println(v) fmt.Println(v)
@ -1268,18 +1272,21 @@ func TestFoo(t *testing.T) {
</pre> </pre>
<p> <p>
Run <code>gotest</code> in that directory. Run <code>go test</code> in that directory.
That script finds the <code>Test</code> functions, That script finds the <code>Test</code> functions,
builds a test binary, and runs it. builds a test binary, and runs it.
</p> </p>
<p>See the <a href="/doc/code.html">How to Write Go Code</a> document for more details.</p> <p>See the <a href="/doc/code.html">How to Write Go Code</a> document,
the <a href="/pkg/testing/"><code>testing</code></a> package
and the <a href="/cmd/go/#Test_packages"><code>go test</code></a> subcommand for more details.
</p>
<h3 id="testing_framework"> <h3 id="testing_framework">
Where is my favorite helper function for testing?</h3> Where is my favorite helper function for testing?</h3>
<p> <p>
Go's standard <code>testing</code> package makes it easy to write unit tests, but it lacks Go's standard <a href="/pkg/testing/"><code>testing</code></a> package makes it easy to write unit tests, but it lacks
features provided in other language's testing frameworks such as assertion functions. features provided in other language's testing frameworks such as assertion functions.
An <a href="#assertions">earlier section</a> of this document explained why Go An <a href="#assertions">earlier section</a> of this document explained why Go
doesn't have assertions, and doesn't have assertions, and
@ -1371,9 +1378,9 @@ type checks, reflection, and even panic-time stack traces.
<p> <p>
A trivial C "hello, world" program compiled and linked statically using gcc A trivial C "hello, world" program compiled and linked statically using gcc
on Linux is around 750 kB. An equivalent Go program is around 1.1 MB, but on Linux is around 750 kB. An equivalent Go program using <code>fmt.Printf</code>
that includes more powerful run-time support. We believe that with some effort is around 1.3 MB, but
the size of Go binaries can be reduced. that includes more powerful run-time support.
</p> </p>
<h3 id="unused_variables_and_imports"> <h3 id="unused_variables_and_imports">
@ -1438,7 +1445,7 @@ Why does Go perform badly on benchmark X?</h3>
<p> <p>
One of Go's design goals is to approach the performance of C for comparable One of Go's design goals is to approach the performance of C for comparable
programs, yet on some benchmarks it does quite poorly, including several programs, yet on some benchmarks it does quite poorly, including several
in <a href="/test/bench/">test/bench</a>. The slowest depend on libraries in <a href="/test/bench/shootout/">test/bench/shootout</a>. The slowest depend on libraries
for which versions of comparable performance are not available in Go. for which versions of comparable performance are not available in Go.
For instance, <a href="/test/bench/shootout/pidigits.go">pidigits.go</a> For instance, <a href="/test/bench/shootout/pidigits.go">pidigits.go</a>
depends on a multi-precision math package, and the C depends on a multi-precision math package, and the C
@ -1467,7 +1474,10 @@ garbage can have a huge effect.)
</p> </p>
<p> <p>
In any case, Go can often be very competitive. See the blog post about In any case, Go can often be very competitive.
There has been significant improvement in the performance of many programs
as the language and tools have developed.
See the blog post about
<a href="http://blog.golang.org/2011/06/profiling-go-programs.html">profiling <a href="http://blog.golang.org/2011/06/profiling-go-programs.html">profiling
Go programs</a> for an informative example. Go programs</a> for an informative example.