download:Kubernetes 入门到进阶实战
K8s,容器编排的事实标准,云原生时代的企业技术战略重点,也是未来后端开发、运维必备技能。但 K8s 包含技术点繁多复杂,学起来并不容易。本课程将提供一条更容易系统掌握 K8s 的学习路线,让大家学得轻松,学得透彻
。
技术要求
具备一定服务端基础知识及基本 linux 命令
对容器领域感兴趣的开发者和运维工程师
环境参数
Virtualbox 6
Centos 7
Docker 1.19
Kubernetes 1.19.3
sorter.go:
//主程序// 主程序的package必需是mainpackage 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.gopackage qsortimport "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.gopackage bubblesortfunc BubbleSort(values []int) { flag := true 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 } // end if } // end for j = ... if flag == true { break } } // end for i = ...}
qsort_test.go:
// qsort_test.gopackage qsortimport "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.gopackage bubblesortimport "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