bin存放可执行文件,pkg存放由包生成的库文件,src存放源包文件
|——
|——
|——
|——
|——
sorter.go (main存放处)
|——
|——
bubblesort.go
bubblesort_test.go
|——
qsort.go
qsort_test.go
我在/home/tyliang/go_program目录下生成sorter目录存放整个工程文件。
环境变量于是需要添加成如下形式:
export GOPATH=$HOME/go_program/sorter
需要注意,需要将go源文件统一放到src目录下,因为上述环境变量设置后,会自动在../go_program/sorter/src进行搜索文件。
于是在../go_program/sorter/下还需要生成一个src目录,如上结构进行目录生成和go源文件存放。
编译包文件命令:go build algorithms/bubblesort
go build algorithms/qsort
进行测试命令:go test algorithms/bubblesort
go test algorithms/qsort
进行库文件生成(会被放置到pkg目录下)命令:go install algorithms/bubblesort
go install algorithms/qsort
最后编译main函数:go build sorter
生成可执行文件存(会被放置到bin目录下)命令:go install sorter
sorter.go
package main
import"flag"
import"fmt"
import"bufio"
import"io"
import"os"
import"strconv"
import"time"
import"algorithms/bubblesort"
import"algorithms/qsort"
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)//array slice
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)//convert character array to string
value,err1:=strconv.Atoi(str)//convert string to int
if err1!=nil{
err=err1
return
}
values=append(values,value)
}
return values,err
}
func writeValues(values []int,outfile string) error{
file,err:=os.Create(outfile)
if err!=nil{
fmt.Println("Failed to create the outfile file ", outfile)
return err
}
defer file.Close()
for _,value:=range values{
str:=strconv.Itoa(value)
file.WriteString(str+"\n")
}
return nil
}
func main(){
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)
}
}
bubblesort.go
package bubblesort
func BubbleSort(values []int){
for i:=0;ivalues[j+1]{
values[j],values[j+1]=values[j+1],values[j]
flag=false
}
}
if flag==true{
break
}
}
}
bubblesort_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")
}
}
qsort.go
package qsort
func quickSort(values []int,start,end int){
tmp:=values[start]
low,high:=start,end
for low!=high{
for high>low && values[high]>=tmp{
high--
}
if high>low && values[high]low && values[low]<=tmp{
low++
}
if high>low && values[low]>tmp{
values[high]=values[low]
high--
}
}
values[low]=tmp
if low-start>1{
quickSort(values,start,low-1)
}
if end-low>1{
quickSort(values,low+1,end)
}
}
func QuickSort(values []int){
quickSort(values,0,len(values)-1)
}
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, "Expected5")
}
}