summaryrefslogtreecommitdiff
path: root/doc/progs
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2011-04-18 10:51:40 -0700
committerRob Pike <r@golang.org>2011-04-18 10:51:40 -0700
commitdb7ccfcd2a4e9ab88be022d88a1f099daaa749f1 (patch)
treeed75488f1821c28bcf33119fb0bec00f93fda150 /doc/progs
parentacb67f9fe5d1f209e3cd5d5070265cafc0d42168 (diff)
downloadgo-db7ccfcd2a4e9ab88be022d88a1f099daaa749f1.tar.gz
tutorial: modernize the definition and use of Open.
R=golang-dev, iant CC=golang-dev http://codereview.appspot.com/4446053
Diffstat (limited to 'doc/progs')
-rw-r--r--doc/progs/cat.go3
-rw-r--r--doc/progs/cat_rot13.go3
-rw-r--r--doc/progs/file.go17
-rw-r--r--doc/progs/helloworld3.go2
4 files changed, 21 insertions, 4 deletions
diff --git a/doc/progs/cat.go b/doc/progs/cat.go
index 697e5f786..9f0b8d4a3 100644
--- a/doc/progs/cat.go
+++ b/doc/progs/cat.go
@@ -24,6 +24,7 @@ func cat(f *file.File) {
case nr > 0:
if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f.String(), ew.String())
+ os.Exit(1)
}
}
}
@@ -35,7 +36,7 @@ func main() {
cat(file.Stdin)
}
for i := 0; i < flag.NArg(); i++ {
- f, err := file.Open(flag.Arg(i), 0, 0)
+ f, err := file.Open(flag.Arg(i))
if f == nil {
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err)
os.Exit(1)
diff --git a/doc/progs/cat_rot13.go b/doc/progs/cat_rot13.go
index 03fc02259..0eefe7cfc 100644
--- a/doc/progs/cat_rot13.go
+++ b/doc/progs/cat_rot13.go
@@ -67,6 +67,7 @@ func cat(r reader) {
nw, ew := file.Stdout.Write(buf[0:nr])
if nw != nr {
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", r.String(), ew.String())
+ os.Exit(1)
}
}
}
@@ -78,7 +79,7 @@ func main() {
cat(file.Stdin)
}
for i := 0; i < flag.NArg(); i++ {
- f, err := file.Open(flag.Arg(i), 0, 0)
+ f, err := file.Open(flag.Arg(i))
if f == nil {
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err)
os.Exit(1)
diff --git a/doc/progs/file.go b/doc/progs/file.go
index df3a3cf71..2875ce73a 100644
--- a/doc/progs/file.go
+++ b/doc/progs/file.go
@@ -27,7 +27,7 @@ var (
Stderr = newFile(syscall.Stderr, "/dev/stderr")
)
-func Open(name string, mode int, perm uint32) (file *File, err os.Error) {
+func OpenFile(name string, mode int, perm uint32) (file *File, err os.Error) {
r, e := syscall.Open(name, mode, perm)
if e != 0 {
err = os.Errno(e)
@@ -35,6 +35,21 @@ func Open(name string, mode int, perm uint32) (file *File, err os.Error) {
return newFile(r, name), err
}
+const (
+ O_RDONLY = syscall.O_RDONLY
+ O_RDWR = syscall.O_RDWR
+ O_CREATE = syscall.O_CREAT
+ O_TRUNC = syscall.O_TRUNC
+)
+
+func Open(name string) (file *File, err os.Error) {
+ return OpenFile(name, O_RDONLY, 0)
+}
+
+func Create(name string) (file *File, err os.Error) {
+ return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
+}
+
func (file *File) Close() os.Error {
if file == nil {
return os.EINVAL
diff --git a/doc/progs/helloworld3.go b/doc/progs/helloworld3.go
index adbcea324..5bb0be218 100644
--- a/doc/progs/helloworld3.go
+++ b/doc/progs/helloworld3.go
@@ -13,7 +13,7 @@ import (
func main() {
hello := []byte("hello, world\n")
file.Stdout.Write(hello)
- f, err := file.Open("/does/not/exist", 0, 0)
+ f, err := file.Open("/does/not/exist")
if f == nil {
fmt.Printf("can't open file; err=%s\n", err.String())
os.Exit(1)