Go 语言教程中的一个示例,用的装饰模式,实现了 Linux 的 cat 功能,还有一个 rot13 选项,可以对字符串进行简单加密。
实现了 reader 接口的 File 对象。
file.go
// 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 int // file descriptor number name string // file name at Open time } func newFile(fd int, 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 int, perm int) (file *File, err os.Error) { r, e := syscall.Open(name, mode, perm) if e != 0 { err = os.Errno(e) } return newFile(r, name), err } func (file *File) Close() os.Error { if file == nil { return os.EINVAL } e := syscall.Close(file.fd) file.fd = -1 // 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 }
reader 接口及装饰后的对象 rotate13,
cat_rot13.go
// 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 main import ( "./file" "flag" "fmt" "os" ) var rot13Flag = flag.Bool("rot13", false, "rot13 the input") func rot13(b byte) byte { if 'a' <= b && b <= 'z' { b = 'a' + ((b - 'a') + 13) % 26 } if 'A' <= b && b <= 'Z' { b = 'A' + ((b - 'A') + 13) % 26 } return b } type reader interface { Read(b []byte) (ret int, err os.Error) String() string } type rotate13 struct { source reader } func newRotate13(source reader) *rotate13 { return &rotate13{source} } func (r13 *rotate13) Read(b []byte) (ret int, err os.Error) { r, e := r13.source.Read(b) for i := 0; i < r; i++ { b[i] = rot13(b[i]) } return r, e } func (r13 *rotate13) String() string { return r13.source.String() } // end of rotate13 implementation func cat(r reader) { const NBUF = 512 var buf [NBUF]byte
if *rot13Flag { r = newRotate13(r) // 用 rotate13 装饰 r,使得所有输出自动改变。 } for { switch nr, er := r.Read(&buf); { case nr < 0: fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", r.String(), er.String()) os.Exit(1) case nr == 0: // EOF return case nr > 0: 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()) } } } } func main() { flag.Parse() // Scans the arg list and sets up flags if flag.NArg() == 0 { cat(file.Stdin) } for i := 0; i < flag.NArg(); i++ { f, err := file.Open(flag.Arg(i), 0, 0) if f == nil { fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err) os.Exit(1) } cat(f) f.Close() } }
有点长,但是用得很恰当,呵呵