diff options
Diffstat (limited to 'doc/progs')
| -rw-r--r-- | doc/progs/eff_bytesize.go | 47 | ||||
| -rw-r--r-- | doc/progs/eff_qr.go | 46 | ||||
| -rw-r--r-- | doc/progs/eff_sequence.go | 42 | ||||
| -rw-r--r-- | doc/progs/file_windows.go | 2 | ||||
| -rwxr-xr-x | doc/progs/run | 8 |
5 files changed, 143 insertions, 2 deletions
diff --git a/doc/progs/eff_bytesize.go b/doc/progs/eff_bytesize.go new file mode 100644 index 000000000..bcfde1a5a --- /dev/null +++ b/doc/progs/eff_bytesize.go @@ -0,0 +1,47 @@ +// 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 "fmt" + +type ByteSize float64 + +const ( + _ = iota // ignore first value by assigning to blank identifier + KB ByteSize = 1 << (10 * iota) + MB + GB + TB + PB + EB + ZB + YB +) + +func (b ByteSize) String() string { + switch { + case b >= YB: + return fmt.Sprintf("%.2fYB", float64(b/YB)) + case b >= ZB: + return fmt.Sprintf("%.2fZB", float64(b/ZB)) + case b >= EB: + return fmt.Sprintf("%.2fEB", float64(b/EB)) + case b >= PB: + return fmt.Sprintf("%.2fPB", float64(b/PB)) + case b >= TB: + return fmt.Sprintf("%.2fTB", float64(b/TB)) + case b >= GB: + return fmt.Sprintf("%.2fGB", float64(b/GB)) + case b >= MB: + return fmt.Sprintf("%.2fMB", float64(b/MB)) + case b >= KB: + return fmt.Sprintf("%.2fKB", float64(b/KB)) + } + return fmt.Sprintf("%.2fB", float64(b)) +} + +func main() { + fmt.Println(YB, ByteSize(1e13)) +} diff --git a/doc/progs/eff_qr.go b/doc/progs/eff_qr.go new file mode 100644 index 000000000..5d1fd38e0 --- /dev/null +++ b/doc/progs/eff_qr.go @@ -0,0 +1,46 @@ +package main + +import ( + "flag" + "http" + "log" + "template" +) + +var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18 + +var templ = template.Must(template.New("qr").Parse(templateStr)) + +func main() { + flag.Parse() + http.Handle("/", http.HandlerFunc(QR)) + err := http.ListenAndServe(*addr, nil) + if err != nil { + log.Fatal("ListenAndServe:", err) + } +} + +func QR(w http.ResponseWriter, req *http.Request) { + templ.Execute(w, req.FormValue("s")) +} + +const templateStr = ` +<html> +<head> +<title>QR Link Generator</title> +</head> +<body> +{{if .}} +<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={{urlquery .}}" /> +<br> +{{html .}} +<br> +<br> +{{end}} +<form action="/" name=f method="GET"><input maxLength=1024 size=70 +name=s value="" title="Text to QR Encode"><input type=submit +value="Show QR" name=qr> +</form> +</body> +</html> +` diff --git a/doc/progs/eff_sequence.go b/doc/progs/eff_sequence.go new file mode 100644 index 000000000..11c885abf --- /dev/null +++ b/doc/progs/eff_sequence.go @@ -0,0 +1,42 @@ +// 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 ( + "fmt" + "sort" +) + +func main() { + seq := Sequence{6, 2, -1, 44, 16} + sort.Sort(seq) + fmt.Println(seq) +} + +type Sequence []int + +// Methods required by sort.Interface. +func (s Sequence) Len() int { + return len(s) +} +func (s Sequence) Less(i, j int) bool { + return s[i] < s[j] +} +func (s Sequence) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +// Method for printing - sorts the elements before printing. +func (s Sequence) String() string { + sort.Sort(s) + str := "[" + for i, elem := range s { + if i > 0 { + str += " " + } + str += fmt.Sprint(elem) + } + return str + "]" +} diff --git a/doc/progs/file_windows.go b/doc/progs/file_windows.go index 03003a3f7..bfbac75ad 100644 --- a/doc/progs/file_windows.go +++ b/doc/progs/file_windows.go @@ -15,7 +15,7 @@ type File struct { } func newFile(fd syscall.Handle, name string) *File { - if fd < 0 { + if fd == ^syscall.Handle(0) { return nil } return &File{fd, name} diff --git a/doc/progs/run b/doc/progs/run index 81781c9d2..9d56049a8 100755 --- a/doc/progs/run +++ b/doc/progs/run @@ -15,7 +15,7 @@ fi rm -f *.$O if [ "$GOOS" = "windows" ];then - $GC -o file.8 file_windows.go + $GC -o file.$O file_windows.go else $GC file.go fi @@ -35,6 +35,9 @@ for i in \ sieve1.go \ server1.go \ strings.go \ + eff_bytesize.go\ + eff_qr.go \ + eff_sequence.go\ ; do $GC $i done @@ -82,4 +85,7 @@ testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29" $GC server.go testit server1 "" "" +testit eff_bytesize "" "1.00YB 9.09TB" +testit eff_sequence "" "[-1 2 6 16 44]" + rm -f $O.out *.$O |
