diff options
Diffstat (limited to 'doc/progs')
-rw-r--r-- | doc/progs/file_windows.go | 89 | ||||
-rwxr-xr-x | doc/progs/run | 7 | ||||
-rw-r--r-- | doc/progs/sort.go | 36 | ||||
-rw-r--r-- | doc/progs/sortmain.go | 4 |
4 files changed, 115 insertions, 21 deletions
diff --git a/doc/progs/file_windows.go b/doc/progs/file_windows.go new file mode 100644 index 000000000..d5e7c00d3 --- /dev/null +++ b/doc/progs/file_windows.go @@ -0,0 +1,89 @@ +// 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 syscall.Handle // file descriptor number + name string // file name at Open time +} + +func newFile(fd syscall.Handle, name string) *File { + if fd < 0 { + return nil + } + return &File{fd, name} +} + +var ( + Stdin = newFile(syscall.Stdin, "/dev/stdin") + Stdout = newFile(syscall.Stdout, "/dev/stdout") + Stderr = newFile(syscall.Stderr, "/dev/stderr") +) + +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) + } + 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 + } + e := syscall.Close(file.fd) + file.fd = syscall.InvalidHandle // so it can't be closed again + if e != 0 { + return os.Errno(e) + } + 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) + if e != 0 { + err = os.Errno(e) + } + return int(r), err +} + +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) + if e != 0 { + err = os.Errno(e) + } + return int(r), err +} + +func (file *File) String() string { + return file.name +} diff --git a/doc/progs/run b/doc/progs/run index 241e65dfa..81781c9d2 100755 --- a/doc/progs/run +++ b/doc/progs/run @@ -14,8 +14,13 @@ fi rm -f *.$O +if [ "$GOOS" = "windows" ];then + $GC -o file.8 file_windows.go +else + $GC file.go +fi + for i in \ - file.go \ helloworld.go \ helloworld3.go \ echo.go \ diff --git a/doc/progs/sort.go b/doc/progs/sort.go index 79e7f563e..47df9b351 100644 --- a/doc/progs/sort.go +++ b/doc/progs/sort.go @@ -30,34 +30,34 @@ func IsSorted(data Interface) bool { // Convenience types for common cases -type IntArray []int +type IntSlice []int -func (p IntArray) Len() int { return len(p) } -func (p IntArray) Less(i, j int) bool { return p[i] < p[j] } -func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p IntSlice) Len() int { return len(p) } +func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] } +func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -type Float64Array []float64 +type Float64Slice []float64 -func (p Float64Array) Len() int { return len(p) } -func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] } -func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p Float64Slice) Len() int { return len(p) } +func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] } +func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } -type StringArray []string +type StringSlice []string -func (p StringArray) Len() int { return len(p) } -func (p StringArray) Less(i, j int) bool { return p[i] < p[j] } -func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] } +func (p StringSlice) Len() int { return len(p) } +func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] } +func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } // Convenience wrappers for common cases -func SortInts(a []int) { Sort(IntArray(a)) } -func SortFloat64s(a []float64) { Sort(Float64Array(a)) } -func SortStrings(a []string) { Sort(StringArray(a)) } +func SortInts(a []int) { Sort(IntSlice(a)) } +func SortFloat64s(a []float64) { Sort(Float64Slice(a)) } +func SortStrings(a []string) { Sort(StringSlice(a)) } -func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) } -func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) } -func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) } +func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) } +func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) } +func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) } diff --git a/doc/progs/sortmain.go b/doc/progs/sortmain.go index a77ae7381..28eec8d4f 100644 --- a/doc/progs/sortmain.go +++ b/doc/progs/sortmain.go @@ -11,7 +11,7 @@ import ( func ints() { data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586} - a := sort.IntArray(data) + a := sort.IntSlice(data) sort.Sort(a) if !sort.IsSorted(a) { panic("fail") @@ -20,7 +20,7 @@ func ints() { func strings() { data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"} - a := sort.StringArray(data) + a := sort.StringSlice(data) sort.Sort(a) if !sort.IsSorted(a) { panic("fail") |