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)

iferr != 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()

iferr1 != 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)

iferr1 != nil {

err = err1

return

}

values = append(values, value)

}

return

}

func writeValues(values []int, outfile string) error {

file, err := os.Create(outfile)

iferr != 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()

ifinfile != nil {

fmt.Println(“infile =”, *infile, “outfile =”, *outfile, “algorithm =”,

*algorithm)

}

values, err := readValues(*infile)

iferr == 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

fori := 0; i

flag = true

forj := 0; j

ifvalues[j] > values[j + 1] {

values[j], values[j + 1] = values[j + 1], values[j]

flag = false

} // end if

} // end for j = …

ifflag == 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/qsortgo build sorter/algorithms/bubblesortgo test sorter/algorithms/qsortgo test sorter/algorithms/bubblesortgo install sorter/algorithms/qsortgo install sorter/algorithms/bubblesortgo build sortergo install sorter

运行结果:

输出文件:sorted.dat

你可能感兴趣的:(Go语言学习之完整代码样例)