Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(34)

Side by Side Diff: doc/progs/file.go

Issue 5697077: code review 5697077: tutorial: delete (Closed)
Patch Set: diff -r 0ea926cdbb36 https://code.google.com/p/go/ Created 13 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « doc/progs/echo.go ('k') | doc/progs/file_windows.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package file
6
7 import (
8 "os"
9 "syscall"
10 )
11
12 type File struct {
13 fd int // file descriptor number
14 name string // file name at Open time
15 }
16
17 func newFile(fd int, name string) *File {
18 if fd < 0 {
19 return nil
20 }
21 return &File{fd, name}
22 }
23
24 var (
25 Stdin = newFile(syscall.Stdin, "/dev/stdin")
26 Stdout = newFile(syscall.Stdout, "/dev/stdout")
27 Stderr = newFile(syscall.Stderr, "/dev/stderr")
28 )
29
30 func OpenFile(name string, mode int, perm uint32) (file *File, err error) {
31 r, err := syscall.Open(name, mode, perm)
32 return newFile(r, name), err
33 }
34
35 const (
36 O_RDONLY = syscall.O_RDONLY
37 O_RDWR = syscall.O_RDWR
38 O_CREATE = syscall.O_CREAT
39 O_TRUNC = syscall.O_TRUNC
40 )
41
42 func Open(name string) (file *File, err error) {
43 return OpenFile(name, O_RDONLY, 0)
44 }
45
46 func Create(name string) (file *File, err error) {
47 return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666)
48 }
49
50 func (file *File) Close() error {
51 if file == nil {
52 return os.ErrInvalid
53 }
54 err := syscall.Close(file.fd)
55 file.fd = -1 // so it can't be closed again
56 return err
57 }
58
59 func (file *File) Read(b []byte) (ret int, err error) {
60 if file == nil {
61 return -1, os.ErrInvalid
62 }
63 r, err := syscall.Read(file.fd, b)
64 return int(r), err
65 }
66
67 func (file *File) Write(b []byte) (ret int, err error) {
68 if file == nil {
69 return -1, os.ErrInvalid
70 }
71 r, err := syscall.Write(file.fd, b)
72 return int(r), err
73 }
74
75 func (file *File) String() string {
76 return file.name
77 }
OLDNEW
« no previous file with comments | « doc/progs/echo.go ('k') | doc/progs/file_windows.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b