Go语言学习之完整代码样例

参考:《Go语言编程》

 

代码结构:

Go语言学习之完整代码样例_第1张图片

sorter.go:

//主程序
// 主程序的package必须是main
package main

//import 不用管,IDE会自动增加
import (
	"flag"
	"os"
	"fmt"
	"bufio"
	"io"
	"strconv"
	"time"
	"sorter/algorithms/qsort"
	"sorter/algorithms/bubblesort"
)

////flag包快迅解析命令行参数的
var infile *string = flag.String("i", "infile", "File contains values for sorting")
var outfile *string = flag.String("o", "outfile", "File to receive sorted values")
var algorithm *string = flag.String("a", "qsort", "Sort algorithm")

func readValues(infile string)(values []int, err error) {
	file, err := os.Open(infile)
	if err != nil {
		fmt.Println("Failed to open the input file ", infile)
		return
	}
	defer file.Close()
	br := bufio.NewReader(file)
	values = make([]int, 0)
	for {
		line, isPrefix, err1 := br.ReadLine()
		if err1 != nil {
			if err1 != io.EOF {
				err = err1
			}
			break
		}
		if isPrefix {
			fmt.Println("A too long line, seems unexpected.")
			return
		}
		str := string(line) // 转换字符数组为字符串
		value, err1 := strconv.Atoi(str)
		if err1 != nil {
			err = err1
			return
		}
		values = append(values, value)
	}
	return
}

func writeValues(values []int, outfile string) error {
	file, err := os.Create(outfile)
	if err != nil {
		fmt.Println("Failed to create the output file ", outfile)
		return err
	}
	defer file.Close()
	for _, value := range values {
		str := strconv.Itoa(value)
		file.WriteString(str + "\n")
	}
	return nil
}

//程序入口
func main() {
	//flag包快迅解析命令行参数的
	flag.Parse()
	if infile != nil {
		fmt.Println("infile =", *infile, "outfile =", *outfile, "algorithm =",
			*algorithm)
	}
	values, err := readValues(*infile)
	if err == nil {
		t1 := time.Now()
		switch *algorithm {
		case "qsort":
			qsort.QuickSort(values)
		case "bubblesort":
			bubblesort.BubbleSort(values)
		default:
			fmt.Println("Sorting algorithm", *algorithm, "is either unknown or unsupported.")
		}
		t2 := time.Now()
		fmt.Println("The sorting process costs", t2.Sub(t1), "to complete.")
		writeValues(values, *outfile)
	} else {
		fmt.Println(err)
	}
}


qsort.go:

// qsort_test.go
package qsort

import "testing"
func TestQuickSort1(t *testing.T) {
	values := []int{5, 4, 3, 2, 1}
	QuickSort(values)
	if values[0] != 1 || values[1] != 2 || values[2] != 3 || values[3] != 4 ||
		values[4] !=5 {
		t.Error("QuickSort() failed. Got", values, "Expected 1 2 3 4 5")
	}
}
func TestQuickSort2(t *testing.T) {
	values := []int{5, 5, 3, 2, 1}
	QuickSort(values)
	if values[0] != 1 || values[1] != 2 || values[2] != 3 || values[3] != 5 ||
		values[4] !=5 {
		t.Error("QuickSort() failed. Got", values, "Expected 1 2 3 5 5")
	}
}
func TestQuickSort3(t *testing.T) {
	values := []int{5}
	QuickSort(values)
	if values[0] != 5 {
		t.Error("QuickSort() failed. Got", values, "Expected 5")
	}
}

bubblesort.go:

// bubblesort.go
package bubblesort
func BubbleSort(values []int) {
	flag := true
	for i := 0; i  values[j + 1] {
				values[j], values[j + 1] = values[j + 1], values[j]
				flag = false
			} // end if
		} // end for j = ...
		if flag == true {
			break
		}
	} // end for i = ...
}
qsort_test.go:
// qsort_test.go
package qsort

import "testing"
func TestQuickSort1(t *testing.T) {
	values := []int{5, 4, 3, 2, 1}
	QuickSort(values)
	if values[0] != 1 || values[1] != 2 || values[2] != 3 || values[3] != 4 ||
		values[4] !=5 {
		t.Error("QuickSort() failed. Got", values, "Expected 1 2 3 4 5")
	}
}
func TestQuickSort2(t *testing.T) {
	values := []int{5, 5, 3, 2, 1}
	QuickSort(values)
	if values[0] != 1 || values[1] != 2 || values[2] != 3 || values[3] != 5 ||
		values[4] !=5 {
		t.Error("QuickSort() failed. Got", values, "Expected 1 2 3 5 5")
	}
}
func TestQuickSort3(t *testing.T) {
	values := []int{5}
	QuickSort(values)
	if values[0] != 5 {
		t.Error("QuickSort() failed. Got", values, "Expected 5")
	}
}

bubble_test.go:
// bubble_test.go
package bubblesort
import "testing"
func TestBubbleSort1(t *testing.T) {
	values := []int{5, 4, 3, 2, 1}
	BubbleSort(values)
	if values[0] != 1 || values[1] != 2 || values[2] != 3 || values[3] != 4 ||
		values[4] !=5 {
		t.Error("BubbleSort() failed. Got", values, "Expected 1 2 3 4 5")
	}
}
func TestBubbleSort2(t *testing.T) {
	values := []int{5, 5, 3, 2, 1}
	BubbleSort(values)
	if values[0] != 1 || values[1] != 2 || values[2] != 3 || values[3] != 5 ||
		values[4] !=5 {
		t.Error("BubbleSort() failed. Got", values, "Expected 1 2 3 5 5")
	}
}
func TestBubbleSort3(t *testing.T) {
	values := []int{5}
	BubbleSort(values)
	if values[0] != 5 {
		t.Error("BubbleSort() failed. Got", values, "Expected 5")
	}
}

运行前准备:

准备“unsorted.dat”文件。

运行过程:

go build sorter/slgorithms/qsort
go build sorter/algorithms/bubblesort
go test sorter/algorithms/qsort
go test sorter/algorithms/bubblesort
go install sorter/algorithms/qsort
go install sorter/algorithms/bubblesort
go build sorter
go install sorter

 

运行结果:

输出文件:sorted.dat

 

 

 

 

 

 

 

你可能感兴趣的:(golang)