summaryrefslogtreecommitdiff
path: root/doc/progs/file.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-03-16 22:53:23 -0700
committerRob Pike <r@golang.org>2009-03-16 22:53:23 -0700
commitfff0c79311dfed21fed1f0445beaa628e4a4cb19 (patch)
treea2d542b0910e356955c2f2fe4f57b5df48edffc4 /doc/progs/file.go
parent834f8e784143f7cf18ea96ab4259edd021cd6a5c (diff)
downloadgolang-fff0c79311dfed21fed1f0445beaa628e4a4cb19.tar.gz
change the tutorial to use File, file rather than FD, fd.
also make the default input for makehtml be go_tutorial.txt. R=rsc DELTA=176 (58 added, 58 deleted, 60 changed) OCL=26374 CL=26374
Diffstat (limited to 'doc/progs/file.go')
-rw-r--r--doc/progs/file.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/doc/progs/file.go b/doc/progs/file.go
new file mode 100644
index 000000000..e2ecf9209
--- /dev/null
+++ b/doc/progs/file.go
@@ -0,0 +1,62 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package file
+
+import (
+ "os";
+ "syscall";
+)
+
+type File struct {
+ fd int64; // file descriptor number
+ name string; // file name at Open time
+}
+
+func newFile(fd int64, name string) *File {
+ if fd < 0 {
+ return nil
+ }
+ return &File{fd, name}
+}
+
+var (
+ Stdin = newFile(0, "/dev/stdin");
+ Stdout = newFile(1, "/dev/stdout");
+ Stderr = newFile(2, "/dev/stderr");
+)
+
+func Open(name string, mode int64, perm int64) (file *File, err *os.Error) {
+ r, e := syscall.Open(name, mode, perm);
+ return newFile(r, name), os.ErrnoToError(e)
+}
+
+func (file *File) Close() *os.Error {
+ if file == nil {
+ return os.EINVAL
+ }
+ r, e := syscall.Close(file.fd);
+ file.fd = -1; // so it can't be closed again
+ return nil
+}
+
+func (file *File) Read(b []byte) (ret int, err *os.Error) {
+ if file == nil {
+ return -1, os.EINVAL
+ }
+ r, e := syscall.Read(file.fd, &b[0], int64(len(b)));
+ return int(r), os.ErrnoToError(e)
+}
+
+func (file *File) Write(b []byte) (ret int, err *os.Error) {
+ if file == nil {
+ return -1, os.EINVAL
+ }
+ r, e := syscall.Write(file.fd, &b[0], int64(len(b)));
+ return int(r), os.ErrnoToError(e)
+}
+
+func (file *File) String() string {
+ return file.name
+}