From 24ce19c71db93ad4cf425a3a3b0f67fe3de8c803 Mon Sep 17 00:00:00 2001
From: Russ Cox
-gofmt
will make the columns line up.
+gofmt
will line up the columns:
@@ -237,7 +237,7 @@ var (Even for private names, grouping can also indicate relationships between items, -such as the fact that a set of variables is controlled by a mutex. +such as the fact that a set of variables is protected by a mutex.
@@ -293,14 +293,14 @@ defines which version is being used. Another convention is that the package name is the base name of its source directory; the package insrc/pkg/container/vector
-is installed as"container/vector"
but has namevector
, +is imported as"container/vector"
but has namevector
, notcontainer_vector
and notcontainerVector
.The importer of a package will use the name to refer to its contents (the
import .
notation is intended mostly for tests and other -unusual situations) and exported names in the package can use that fact +unusual situations), so exported names in the package can use that fact to avoid stutter. For instance, the buffered reader type in thebufio
package is calledReader
, notBufReader
, because users see it asbufio.Reader
, @@ -308,9 +308,9 @@ which is a clear, concise name. Moreover, because imported entities are always addressed with their package name,bufio.Reader
does not conflict withio.Reader
. -Similarly, the function to make new instances ofvector.Vector
-—which is the definition of a constructor in Go—would -normally be calledNewVector
but since +Similarly, the function to make new instances ofvector.Vector
—which +is the definition of a constructor in Go—would +normally be calledNewVector
, but sinceVector
is the only type exported by the package, and since the package is calledvector
, it's called justNew
. Clients of the package see that asvector.New
. @@ -664,11 +664,11 @@ and modifying an argument.-In C, a write error is signaled by a negative byte count with the +In C, a write error is signaled by a negative count with the error code secreted away in a volatile location. In Go,
@@ -765,7 +765,7 @@ They do different things and apply to different types, which can be confusing, but the rules are simple. Let's talk aboutWrite
-can return a byte count and an error: "Yes, you wrote some -bytes but not all of them because you filled the device". +can return a count and an error: “Yes, you wrote some +bytes but not all of them because you filled the device”. The signature of*File.Write
in packageos
is:new()
first. It's a built-in function essentially the same as its namesakes -in other languages: it allocates zeroed storage for a new item of type +in other languages:new(T)
allocates zeroed storage for a new item of typeT
and returns its address, a value of type*T
. In Go terminology, it returns a pointer to a newly allocated zero value of typeT
. @@ -873,18 +873,13 @@ order, with the missing ones left as their respective zero values. Thus we coulAs a limiting case, if a composite literal contains no fields at all, it creates -a zero value for the type. These two expressions are equivalent. +a zero value for the type. The expressions
-new(File)
and&File{}
are equivalent.-new(File) -&File{} --Composite literals can also be created for arrays, slices, and maps, with the field labels being indices or map keys as appropriate. -In these examples, the initializations work regardless of the values of
@@ -945,7 +940,8 @@ v := make([]int, 100);EnoError
, +In these examples, the initializations work regardless of the values ofEnone
,Eio
, andEinval
, as long as they are distinct.-Remember that
@@ -953,7 +949,7 @@ To obtain an explicit pointer allocate withmake()
applies only to maps, slices and channels. +Remember thatmake()
applies only to maps, slices and channels +and does not return a pointer. To obtain an explicit pointer allocate withnew()
.new()
.Arrays are useful when planning the detailed layout of memory and sometimes -can help avoid allocation but primarily +can help avoid allocation, but primarily they are a building block for slices, the subject of the next section. To lay the foundation for that topic, here are a few words about arrays.
@@ -981,7 +977,7 @@ you can pass a pointer to the array.-func Sum(a *[]float) (sum float) { +func Sum(a *[3]float) (sum float) { for _, v := range a { sum += v } @@ -1010,8 +1006,8 @@ slice to another, both refer to the same underlying array. For instance, if a function takes a slice argument, changes it makes to the elements of the slice will be visible to the caller, analogous to passing a pointer to the underlying array. ARead
-function can therefore accept a slice argument rather than a (pointer -to an) array and a count; the length within the slice sets an upper +function can therefore accept a slice argument rather than a pointer +and a count; the length within the slice sets an upper limit of how much data to read. Here is the signature of theRead
method of theFile
type in packageos
: @@ -1085,10 +1081,11 @@ structure holding the pointer, length, and capacity) is passed by value.Maps are a convenient and powerful built-in data structure to associate values of different types. -The key can be of any type that implements equality, such as integers, +The key can be of any type for which the equality operator is defined, +such as integers, floats, strings, pointers, and interfaces (as long as the dynamic type -supports equality), but not structs, arrays or slices -because those types do not have equality defined for them. +supports equality). Structs, arrays and slices cannot be used as map keys, +because equality is not defined on those types. Like slices, maps are a reference type. If you pass a map to a function that changes the contents of the map, the changes will be visible in the caller. @@ -1514,7 +1511,7 @@ A type can implement multiple interfaces. For instance, a collection can be sorted by the routines in package
@@ -1654,7 +1651,7 @@ implementation of thesort
if it implementssort.Interface
, which containsLen()
, -Less(i, j int)
, andSwap(i, j int)
, +Less(i, j int) bool
, andSwap(i, j int)
, and it could also have a custom formatter. In this contrived exampleSequence
satisfies both.Cipher
interface and anyio.Reader
. Because they returnio.Reader
interface values, replacing ECB encryption with CBC encryption is a localized change. The constructor -calls must be edited, but because the code must treat the result only +calls must be edited, but because the surrounding code must treat the result only as anio.Reader
, it won't notice the difference. @@ -2247,16 +2244,16 @@ Once the message buffer is ready, it's sent to the server onserverChan
.-var freelist = make(chan *Buffer, 100) -var server_chan = make(chan *Buffer) +var freeList = make(chan *Buffer, 100) +var serverChan = make(chan *Buffer) func client() { for { - b, ok := <-freeList; // grab one if available - if !ok { // free list empty; allocate a new buffer + b, ok := <-freeList; // grab a buffer if available + if !ok { // if not, allocate a new one b = new(Buffer) } - load(b); // grab the next message, perhaps from the net + load(b); // read next message from the net serverChan <- b; // send to server } } @@ -2393,7 +2390,7 @@ import ( "template"; ) -var addr = flag.String("addr", ":1718", "http service address") // Q = 17, R = 18 +var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18 var fmap = template.FormatterMap{ "html": template.HtmlFormatter, "url+html": UrlHtmlFormatter, @@ -2456,7 +2453,7 @@ server; it blocks while the server runs.
QR
just receives the request, which contains form data, and -executes the template on the data in the field nameds
. +executes the template on the data in the form value nameds
.