go语言下创建的工程文件

1、值得注意的三个目录bin、pkg、src

bin存放可执行文件,pkg存放由包生成的库文件,src存放源包文件

2、以下是所开僻的相关路径及文件存放位置

|——<sorter>

            |——<pkg>

            |——<bin>

            |——<src>

                       |——<sorter>

                                  sorter.go  (main存放处)

                       |——<algorithms>

                                   |——<bubblesort>

                                                bubblesort.go

                                                bubblesort_test.go

                                   |——<qsort>

                                                qsort.go

                                                qsort_test.go

3、正确设置及解读环境变量

我在/home/tyliang/go_program目录下生成sorter目录存放整个工程文件。

环境变量于是需要添加成如下形式:

export GOPATH=$HOME/go_program/sorter

需要注意,需要将go源文件统一放到src目录下,因为上述环境变量设置后,会自动在../go_program/sorter/src进行搜索文件。

于是在../go_program/sorter/下还需要生成一个src目录,如上结构进行目录生成和go源文件存放。

4、在../go_program/sorter/src下进行编译和生成库文件命令

编译包文件命令: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

5、sorter.go、bubblesort.go、bubblesort_test.go、qsort.go、qsort_test.go的源代码如下所示:

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;i<len(values)-1;i++{
		flag:=true
		for j:=0;j<len(values)-i-1;j++{
			if values[j]>values[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]<tmp{
			values[low]=values[high]
			low++
		}

		for 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")
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值