From d39f5aa373a4422f7a5f3ee764fb0f6b0b719d61 Mon Sep 17 00:00:00 2001 From: Ondřej Surý Date: Thu, 30 Jun 2011 15:34:22 +0200 Subject: Imported Upstream version 58 --- doc/go_tutorial.html | 86 ++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) (limited to 'doc/go_tutorial.html') diff --git a/doc/go_tutorial.html b/doc/go_tutorial.html index 4470f2748..aa85134b3 100644 --- a/doc/go_tutorial.html +++ b/doc/go_tutorial.html @@ -28,9 +28,9 @@ Let's start in the usual way:

 
 05    package main
-

+ 07 import fmt "fmt" // Package implementing formatted I/O. -

+ 09 func main() { 10 fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n") 11 } @@ -118,19 +118,19 @@ Next up, here's a version of the Unix utility echo(1):

 
 05    package main
-

+ 07 import ( 08 "os" 09 "flag" // command line option parser 10 ) -

+ 12 var omitNewline = flag.Bool("n", false, "don't print final newline") -

+ 14 const ( 15 Space = " " 16 Newline = "\n" 17 ) -

+ 19 func main() { 20 flag.Parse() // Scans the arg list and sets up flags 21 var s string = "" @@ -340,7 +340,7 @@ Using slices one can write this function (from sum.go): 15 }

-Note how the return type (int) is defined for sum() by stating it +Note how the return type (int) is defined for sum by stating it after the parameter list.

To call the function, we slice the array. This intricate call (we'll show @@ -372,7 +372,7 @@ There are also maps, which you can initialize like this: m := map[string]int{"one":1 , "two":2}

-The built-in function len(), which returns number of elements, +The built-in function len, which returns number of elements, makes its first appearance in sum. It works on strings, arrays, slices, maps, and channels.

@@ -400,7 +400,7 @@ for more examples of its use. Most types in Go are values. If you have an int or a struct or an array, assignment copies the contents of the object. -To allocate a new variable, use new(), which +To allocate a new variable, use the built-in function new, which returns a pointer to the allocated storage.

@@ -417,7 +417,7 @@ or the more idiomatic
 Some types—maps, slices, and channels (see below)—have reference semantics.
 If you're holding a slice or a map and you modify its contents, other variables
 referencing the same underlying data will see the modification.  For these three
-types you want to use the built-in function make():
+types you want to use the built-in function make:
 

     m := make(map[string]int)
@@ -431,11 +431,11 @@ If you just declare the map, as in
 

it creates a nil reference that cannot hold anything. To use the map, -you must first initialize the reference using make() or by assignment from an +you must first initialize the reference using make or by assignment from an existing map.

Note that new(T) returns type *T while make(T) returns type -T. If you (mistakenly) allocate a reference object with new(), +T. If you (mistakenly) allocate a reference object with new rather than make, you receive a pointer to a nil reference, equivalent to declaring an uninitialized variable and taking its address.

@@ -478,12 +478,12 @@ open/close/read/write interface. Here's the start of file.go:

 
 05    package file
-

+ 07 import ( 08 "os" 09 "syscall" 10 ) -

+ 12 type File struct { 13 fd int // file descriptor number 14 name string // file name at Open time @@ -600,7 +600,7 @@ the tricky standard arguments to open and, especially, to create a file: 41 O_CREATE = syscall.O_CREAT 42 O_TRUNC = syscall.O_TRUNC 43 ) -

+ 45 func Open(name string) (file *File, err os.Error) { 46 return OpenFile(name, O_RDONLY, 0) 47 } @@ -631,7 +631,7 @@ each of which declares a receiver variable file. 61 } 62 return nil 63 } -

+ 65 func (file *File) Read(b []byte) (ret int, err os.Error) { 66 if file == nil { 67 return -1, os.EINVAL @@ -642,7 +642,7 @@ each of which declares a receiver variable file. 72 } 73 return int(r), err 74 } -

+ 76 func (file *File) Write(b []byte) (ret int, err os.Error) { 77 if file == nil { 78 return -1, os.EINVAL @@ -653,7 +653,7 @@ each of which declares a receiver variable file. 83 } 84 return int(r), err 85 } -

+ 87 func (file *File) String() string { 88 return file.name 89 } @@ -676,13 +676,13 @@ We can now use our new package:

 
 05    package main
-

+ 07 import ( 08 "./file" 09 "fmt" 10 "os" 11 ) -

+ 13 func main() { 14 hello := []byte("hello, world\n") 15 file.Stdout.Write(hello) @@ -719,14 +719,14 @@ Building on the file package, here's a simple version of the Unix u

 
 05    package main
-

+ 07 import ( 08 "./file" 09 "flag" 10 "fmt" 11 "os" 12 ) -

+ 14 func cat(f *file.File) { 15 const NBUF = 512 16 var buf [NBUF]byte @@ -745,7 +745,7 @@ Building on the file package, here's a simple version of the Unix u 29 } 30 } 31 } -

+ 33 func main() { 34 flag.Parse() // Scans the arg list and sets up flags 35 if flag.NArg() == 0 { @@ -766,7 +766,7 @@ Building on the file package, here's a simple version of the Unix u By now this should be easy to follow, but the switch statement introduces some new features. Like a for loop, an if or switch can include an initialization statement. The switch on line 18 uses one to create variables -nr and er to hold the return values from f.Read(). (The if on line 25 +nr and er to hold the return values from the call to f.Read. (The if on line 25 has the same idea.) The switch statement is general: it evaluates the cases from top to bottom looking for the first case that matches the value; the case expressions don't need to be constants or even integers, as long as @@ -778,14 +778,14 @@ in a for statement, a missing value means true. In fa is a form of if-else chain. While we're here, it should be mentioned that in switch statements each case has an implicit break.

-Line 25 calls Write() by slicing the incoming buffer, which is itself a slice. +Line 25 calls Write by slicing the incoming buffer, which is itself a slice. Slices provide the standard Go way to handle I/O buffers.

Now let's make a variant of cat that optionally does rot13 on its input. It's easy to do by just processing the bytes, but instead we will exploit Go's notion of an interface.

-The cat() subroutine uses only two methods of f: Read() and String(), +The cat subroutine uses only two methods of f: Read and String, so let's start by defining an interface that has exactly those two methods. Here is code from progs/cat_rot13.go:

@@ -810,11 +810,11 @@ we have a second implementation of the reader interface. 31 type rotate13 struct { 32 source reader 33 } -

+ 35 func newRotate13(source reader) *rotate13 { 36 return &rotate13{source} 37 } -

+ 39 func (r13 *rotate13) Read(b []byte) (ret int, err os.Error) { 40 r, e := r13.source.Read(b) 41 for i := 0; i < r; i++ { @@ -822,7 +822,7 @@ we have a second implementation of the reader interface. 43 } 44 return r, e 45 } -

+ 47 func (r13 *rotate13) String() string { 48 return r13.source.String() 49 } @@ -837,13 +837,13 @@ To use the new feature, we define a flag: 14 var rot13Flag = flag.Bool("rot13", false, "rot13 the input")

-and use it from within a mostly unchanged cat() function: +and use it from within a mostly unchanged cat function:

 
 52    func cat(r reader) {
 53        const NBUF = 512
 54        var buf [NBUF]byte
-

+ 56 if *rot13Flag { 57 r = newRotate13(r) 58 } @@ -865,7 +865,7 @@ and use it from within a mostly unchanged cat() function: 74 }

-(We could also do the wrapping in main and leave cat() mostly alone, except +(We could also do the wrapping in main and leave cat mostly alone, except for changing the type of the argument; consider that an exercise.) Lines 56 through 58 set it all up: If the rot13 flag is true, wrap the reader we received into a rotate13 and proceed. Note that the interface variables @@ -936,7 +936,7 @@ arrays of integers, strings, etc.; here's the code for arrays of int

 
 33    type IntArray []int
-

+ 35 func (p IntArray) Len() int { return len(p) } 36 func (p IntArray) Less(i, j int) bool { return p[i] < p[j] } 37 func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] } @@ -969,11 +969,11 @@ to implement the three methods for that type, like this: 32 shortName string 33 longName string 34 } -

+ 36 type dayArray struct { 37 data []*day 38 } -

+ 40 func (p *dayArray) Len() int { return len(p.data) } 41 func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num } 42 func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] } @@ -1054,7 +1054,7 @@ to that of the Printf call above.

If you have your own type you'd like Printf or Print to format, -just give it a String() method that returns a string. The print +just give it a String method that returns a string. The print routines will examine the value to inquire whether it implements the method and if so, use it rather than some other formatting. Here's a simple example. @@ -1064,25 +1064,25 @@ Here's a simple example. 10 a int 11 b string 12 } -

+ 14 func (t *testType) String() string { 15 return fmt.Sprint(t.a) + " " + t.b 16 } -

+ 18 func main() { 19 t := &testType{77, "Sunset Strip"} 20 fmt.Println(t) 21 }

-Since *testType has a String() method, the +Since *testType has a String method, the default formatter for that type will use it and produce the output

     77 Sunset Strip
 

-Observe that the String() method calls Sprint (the obvious Go +Observe that the String method calls Sprint (the obvious Go variant that returns a string) to do its formatting; special formatters can use the fmt library recursively.

@@ -1095,7 +1095,7 @@ and such, but that's getting a little off the main thread so we'll leave it as an exploration exercise.

You might ask, though, how Printf can tell whether a type implements -the String() method. Actually what it does is ask if the value can +the String method. Actually what it does is ask if the value can be converted to an interface variable that implements the method. Schematically, given a value v, it does this:

@@ -1140,7 +1140,7 @@ interface type defined in the io library:

(This interface is another conventional name, this time for Write; there are also io.Reader, io.ReadWriter, and so on.) -Thus you can call Fprintf on any type that implements a standard Write() +Thus you can call Fprintf on any type that implements a standard Write method, not just files but also network channels, buffers, whatever you want.

@@ -1346,7 +1346,7 @@ code that invokes the operation and responds to the request:

 
 14    type binOp func(a, b int) int
-

+ 16 func run(op binOp, req *request) { 17 reply := op(req.a, req.b) 18 req.replyc <- reply -- cgit v1.2.3