链客,专为开发者而生,有问必答!
此文章来自链客区块链技术问答社区,未经允许拒绝转载。
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
}
i