This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go-tour: add Stringer, Reader discussions and exercises; other tweaks
LGTM=campoy R=campoy CC=golang-codereviews https://codereview.appspot.com/118170043
- Loading branch information
Showing
7 changed files
with
193 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
import "code.google.com/p/go-tour/reader" | ||
|
||
type MyReader struct{} | ||
|
||
// TODO: Add a Read(byte) (int, error) method to MyReader. | ||
|
||
func main() { | ||
reader.Validate(MyReader{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type IPAddr [4]byte | ||
|
||
// TODO: Add a "String() string" method to IPAddr. | ||
|
||
func main() { | ||
addrs := map[string]IPAddr{ | ||
"loopback": {127, 0, 0, 1}, | ||
"googleDNS": {8, 8, 8, 8}, | ||
} | ||
for n, a := range addrs { | ||
fmt.Printf("%v: %v\n", n, a) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
r := strings.NewReader("Hello, Reader!") | ||
|
||
b := make([]byte, 8) | ||
for { | ||
n, err := r.Read(b) | ||
fmt.Printf("n = %v err = %v b = %v\n", n, err, b) | ||
fmt.Printf("b[:n] = %q\n", b[:n]) | ||
if err == io.EOF { | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Person struct { | ||
Name string | ||
Age int | ||
} | ||
|
||
func (p Person) String() string { | ||
return fmt.Sprintf("%v (%v years)", p.Name, p.Age) | ||
} | ||
|
||
func main() { | ||
a := Person{"Arthur Dent", 42} | ||
z := Person{"Zaphod Beeblebrox", 9001} | ||
fmt.Println(a, z) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package reader | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
func Validate(r io.Reader) { | ||
b := make([]byte, 1024) | ||
i, o := 0, 0 | ||
for ; i < 1<<20 && o < 1<<20; i++ { // test 1mb | ||
n, err := r.Read(b) | ||
for i, v := range b[:n] { | ||
if v != 'A' { | ||
fmt.Fprintf(os.Stderr, "got byte %x at offset %v, want 'A'\n", v, o+i) | ||
return | ||
} | ||
} | ||
o += n | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "read error: %v\n", err) | ||
return | ||
} | ||
} | ||
if o == 0 { | ||
fmt.Fprintf(os.Stderr, "read zero bytes after %d Read calls\n", i) | ||
return | ||
} | ||
fmt.Println("OK!") | ||
} |