mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
update book code
This commit is contained in:
14
eBook/examples/chapter_13/errors.go
Normal file
14
eBook/examples/chapter_13/errors.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// errors.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var errNotFound error = errors.New("Not found error")
|
||||
|
||||
func main() {
|
||||
fmt.Printf("error: %v", errNotFound)
|
||||
}
|
||||
// error: Not found error
|
10
eBook/examples/chapter_13/even/even/even.go
Normal file
10
eBook/examples/chapter_13/even/even/even.go
Normal file
@@ -0,0 +1,10 @@
|
||||
// even.go
|
||||
package even
|
||||
|
||||
func Even(i int) bool { // Exported function
|
||||
return i%2 == 0
|
||||
}
|
||||
|
||||
func Odd(i int) bool { // Exported function
|
||||
return i%2 != 0
|
||||
}
|
27
eBook/examples/chapter_13/even/even/oddeven_test.go
Normal file
27
eBook/examples/chapter_13/even/even/oddeven_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// oddeven_test.go
|
||||
package even
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEven(t *testing.T) {
|
||||
if !Even(10) {
|
||||
t.Log(" 10 must be even!")
|
||||
t.Fail()
|
||||
}
|
||||
if Even(7) {
|
||||
t.Log(" 7 is not even!")
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestOdd(t *testing.T) {
|
||||
if !Odd(11) {
|
||||
t.Log(" 11 must be odd!")
|
||||
t.Fail()
|
||||
}
|
||||
if Odd(10) {
|
||||
t.Log(" 10 is not odd!")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
13
eBook/examples/chapter_13/even/even_main/even_main.go
Normal file
13
eBook/examples/chapter_13/even/even_main/even_main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// test_oddeven.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"even/even"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for i:=0; i<=100; i++ {
|
||||
fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i))
|
||||
}
|
||||
}
|
61
eBook/examples/chapter_13/exec.go
Normal file
61
eBook/examples/chapter_13/exec.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// exec.go
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1) os.StartProcess //
|
||||
/*********************/
|
||||
/* Linux: */
|
||||
env := os.Environ()
|
||||
procAttr := &os.ProcAttr{
|
||||
Env: env,
|
||||
Files: []*os.File{
|
||||
os.Stdin,
|
||||
os.Stdout,
|
||||
os.Stderr,
|
||||
},
|
||||
}
|
||||
// 1st example: list files
|
||||
pid, err := os.StartProcess("/bin/ls", []string{"ls", "-l"}, procAttr)
|
||||
if err != nil {
|
||||
fmt.Printf("Error %v starting process!", err) //
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("The process id is %v", pid)
|
||||
// 2nd example: show all processes
|
||||
pid, err = os.StartProcess("/bin/ps", []string{"-e", "-opid,ppid,comm"}, procAttr)
|
||||
if err != nil {
|
||||
fmt.Printf("Error %v starting process!", err) //
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("The process id is %v", pid)
|
||||
/* Output 1st:
|
||||
The process id is &{2054 0}total 2056
|
||||
-rwxr-xr-x 1 ivo ivo 1157555 2011-07-04 16:48 Mieken_exec
|
||||
-rw-r--r-- 1 ivo ivo 2124 2011-07-04 16:48 Mieken_exec.go
|
||||
-rw-r--r-- 1 ivo ivo 18528 2011-07-04 16:48 Mieken_exec_go_.6
|
||||
-rwxr-xr-x 1 ivo ivo 913920 2011-06-03 16:13 panic.exe
|
||||
-rw-r--r-- 1 ivo ivo 180 2011-04-11 20:39 panic.go
|
||||
*/
|
||||
|
||||
// 2) exec.Run //
|
||||
/***************/
|
||||
// Linux: OK, but not for ls ?
|
||||
// cmd := exec.Command("ls", "-l") // no error, but doesn't show anything ?
|
||||
// cmd := exec.Command("ls") // no error, but doesn't show anything ?
|
||||
cmd := exec.Command("gedit") // this opens a gedit-window
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
fmt.Printf("Error %v executing command!", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("The command is %v", cmd)
|
||||
// The command is &{/bin/ls [ls -l] [] <nil> <nil> <nil> 0xf840000210 <nil> true [0xf84000ea50 0xf84000e9f0 0xf84000e9c0] [0xf84000ea50 0xf84000e9f0 0xf84000e9c0] [] [] 0xf8400128c0}
|
||||
}
|
||||
// in Windows: uitvoering: Error fork/exec /bin/ls: The system cannot find the path specified. starting process!
|
||||
|
||||
|
9
eBook/examples/chapter_13/panic.go
Normal file
9
eBook/examples/chapter_13/panic.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Starting the program")
|
||||
panic("A severe error occurred: stopping the program!")
|
||||
fmt.Println("Ending the program")
|
||||
}
|
39
eBook/examples/chapter_13/panic_package.go
Normal file
39
eBook/examples/chapter_13/panic_package.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// panic_package.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./parse/parse"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var examples = []string{
|
||||
"1 2 3 4 5",
|
||||
"100 50 25 12.5 6.25",
|
||||
"2 + 2 = 4",
|
||||
"1st class",
|
||||
"",
|
||||
}
|
||||
|
||||
for _, ex := range examples {
|
||||
fmt.Printf("Parsing %q:\n ", ex)
|
||||
nums, err := parse.Parse(ex)
|
||||
if err != nil {
|
||||
fmt.Println(err) // here String() method from ParseError is used
|
||||
continue
|
||||
}
|
||||
fmt.Println(nums)
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
Parsing "1 2 3 4 5":
|
||||
[1 2 3 4 5]
|
||||
Parsing "100 50 25 12.5 6.25":
|
||||
pkg parse: error parsing "12.5" as int
|
||||
Parsing "2 + 2 = 4":
|
||||
pkg parse: error parsing "+" as int
|
||||
Parsing "1st class":
|
||||
pkg parse: error parsing "1st" as int
|
||||
Parsing "":
|
||||
pkg: no words to parse
|
||||
*/
|
32
eBook/examples/chapter_13/panic_recover.go
Normal file
32
eBook/examples/chapter_13/panic_recover.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// panic_recover.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func badCall() {
|
||||
panic("bad end")
|
||||
}
|
||||
|
||||
func test() {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
fmt.Printf("Panicing %s\r\n", e)
|
||||
}
|
||||
}()
|
||||
badCall()
|
||||
fmt.Printf("After bad call\r\n") // <-- wordt niet bereikt
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Calling test\r\n")
|
||||
test()
|
||||
fmt.Printf("Test completed\r\n")
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Calling test
|
||||
Panicing bad end
|
||||
Test completed
|
||||
*/
|
51
eBook/examples/chapter_13/parse/parse.go
Normal file
51
eBook/examples/chapter_13/parse/parse.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// parse.go
|
||||
package parse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// A ParseError indicates an error in converting a word into an integer.
|
||||
type ParseError struct {
|
||||
Index int // The index into the space-separated list of words.
|
||||
Word string // The word that generated the parse error.
|
||||
Err error // The raw error that precipitated this error, if any.
|
||||
}
|
||||
|
||||
// String returns a human-readable error message.
|
||||
func (e *ParseError) String() string {
|
||||
return fmt.Sprintf("pkg parse: error parsing %q as int", e.Word)
|
||||
}
|
||||
|
||||
// Parse parses the space-separated words in in put as integers.
|
||||
func Parse(input string) (numbers []int, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
var ok bool
|
||||
err, ok = r.(error)
|
||||
if !ok {
|
||||
err = fmt.Errorf("pkg: %v", r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
fields := strings.Fields(input)
|
||||
numbers = fields2numbers(fields)
|
||||
return
|
||||
}
|
||||
|
||||
func fields2numbers(fields []string) (numbers []int) {
|
||||
if len(fields) == 0 {
|
||||
panic("no words to parse")
|
||||
}
|
||||
for idx, field := range fields {
|
||||
num, err := strconv.Atoi(field)
|
||||
if err != nil {
|
||||
panic(&ParseError{idx, field, err})
|
||||
}
|
||||
numbers = append(numbers, num)
|
||||
}
|
||||
return
|
||||
}
|
2
eBook/examples/chapter_13/xtime
Normal file
2
eBook/examples/chapter_13/xtime
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
/usr/bin/time -f '%Uu %Ss %er %MkB %C' "$@"
|
Reference in New Issue
Block a user