GO语言(五)项目搭建
<sorter>
|------<src>(手动添加,代码存放处)
|------sorter.go
|------<algorithm>
|--------<qsort>
|-------qsort.go
|-------qsort_test.go
|--------<bubblesort>
|-------bubblesort.go
|-------bubblesort_test.go
|------<pkg> (自动生成,保存静态编译文件)
|------<bin>(自动生成,保存可执行文件)
[注]
<>是目录,*.go是文件;
写5个文件,如下:
------------------------------------------------------------------------------------------
[qsort.go]
1 package qsort 2 3 func quickSort(values []int, left,right int) { 4 temp := values[left] 5 p := left 6 i,j := left,right 7 8 for i<=j { 9 for j >= p && values[j] >= temp { 10 j-- 11 } 12 if j >= p { 13 values[p] = values[j] 14 p = j 15 } 16 17 for i <= p && values[i] <= temp { 18 i++ 19 } 20 if i <= p { 21 values[p] = values[i] 22 p = i 23 } 24 } 25 values[p] = temp 26 if p - left > 1 { 27 quickSort(values, left, p-1) 28 } 29 if right - p > 1 { 30 quickSort(values, p+1, right) 31 } 32 } 33 34 func QuickSort(values []int) { 35 quickSort(values, 0, len(values)-1) 36 }
[qsort_test.go]
1 package qsort 2 3 import "testing" 4 5 func TestQuickSort1(t *testing.T) { 6 values := []int{5,4,3,2,1} 7 QuickSort(values) 8 if values[0] != 1 || values[1] != 2 || values[2] != 3 || values[3] != 4 || values[4] != 5 { 9 t.Error("QuickSort() failed. Got", values, "Expected 1 2 3 4 5") 10 } 11 }
[bubblesort.go]
1 package bubblesort 2 3 func BubbleSort(values []int) { 4 flag := true 5 6 for i:=0; i<len(values); i++ { 7 flag = true 8 9 for j:=0; j<len(values)-i-1; j++{ 10 if values[j] > values[j+1] { 11 values[j],values[j+1] = values[j+1],values[j] 12 flag = false 13 } 14 } 15 16 if(flag == true) { 17 break 18 } 19 } 20 }
[bubblesort_test.go]
1 package bubblesort 2 3 import "testing" 4 5 func TestBubbleSort1(t *testing.T) { 6 values := []int{5,4,3,2,1} 7 BubbleSort(values) 8 if values[0]!=1 || values[1]!=2 || values[2]!=3 || values[3]!=4 || values[4]!=5 { 9 t.Error("BubbleSort() failed.Got", values, "Expected 1 2 3 4 5.") 10 } 11 }
[sorter.go]
1 package main 2 3 import ( 4 "fmt" 5 "flag" 6 "bufio" 7 "io" 8 "os" 9 "strconv" 10 "time" 11 "sorter/algorithm/qsort" 12 "sorter/algorithm/bubblesort" 13 ) 14 15 var infile *string = flag.String("i", "unsorted.dat", "File contains values for sorting") 16 var outfile *string = flag.String("o", "sorted.dat", "File to receive sorted values") 17 var algorithm *string = flag.String("a", "qsort", "Sort algorithm") 18 19 func readValues(infile string)(values []int, err error) { 20 file, err := os.Open(infile) 21 if err != nil { 22 fmt.Println("Failed to open the input file ", infile) 23 return 24 } 25 26 defer file.Close() 27 28 br := bufio.NewReader(file) 29 values = make([]int, 0) 30 31 for { 32 line, isPrefix, errl := br.ReadLine() 33 34 if errl != nil { 35 if errl != io.EOF { 36 err = errl 37 } 38 break 39 } 40 41 if isPrefix { 42 fmt.Println("A too long line, seems unexpected.") 43 return 44 } 45 46 str := string(line) 47 value, errl := strconv.Atoi(str) 48 if errl != nil { 49 err = errl 50 return 51 } 52 values = append(values, value) 53 } 54 return 55 } 56 57 func writeValues(values []int, outfile string) error { 58 file, err := os.Create(outfile) 59 if err != nil { 60 fmt.Println("Failed to create the output file ", outfile) 61 return err 62 } 63 64 defer file.Close() 65 66 for _,value := range values { 67 str := strconv.Itoa(value) 68 file.WriteString(str + "\n") 69 } 70 return nil 71 } 72 73 func main() { 74 flag.Parse() 75 if infile != nil { 76 fmt.Println("infile=", *infile, "outfile=", *outfile, "algorithm=", *algorithm) 77 } 78 79 values, err := readValues(*infile) 80 if err == nil { 81 fmt.Println("Read values: ", values) 82 t1 := time.Now() 83 switch *algorithm { 84 case "qsort" : 85 qsort.QuickSort(values) 86 case "bubblesort": 87 bubblesort.BubbleSort(values) 88 default: 89 fmt.Println("Sorting algorithm", *algorithm, "is either unknown or unsupported.") 90 } 91 t2 := time.Now() 92 93 fmt.Println("The sorting process costs", t2.Sub(t1), "to cpmplete") 94 95 writeValues(values, *outfile) 96 }else { 97 fmt.Println(err) 98 } 99 }
之后,返回到sorter目录节点,依次执行如下命令:(GOPATH:必须加入sorter的目录)
go build sorter/algorithm/qsort
go build sorter/algorithm/bubblesort
go test sorter/algorithm/qsort
go test sorter/algorithm/bubblesort
go install sorter/algorithm/qsort
go install sorter/algorithm/bubblesort
go build sorter
go install sorter
;bin目录下会有sorter的可执行文件生成;
----------------------------------------------------------------------------------------
bin目录下放入一个unsorted.bat文件,文件内容为
23
10
234
89
190
;执行命令:./sorter,会生成一个sorted.bat文件,内容为:
10
23
89
190
234
[完结]