????????????????Go??????????????????????????????( ??? + ??????? + demo )?????????????????????????????????????????????????????????д?????????в?????????????????

?????????????Go?е?????????????????????ο??????????????

????Go?е??????

????Go???????????????????????????(testing) ?? go test ??????????????????????? ?? ???????????????? xxx.go ???????? xxx_test.go ????????????????????????????????????(?μ? %GOROOT%srcpkg ?μ?????)??

????_test.go?????????????(???????μ? go help testfunc )??

1.???????? ??????????
  func TestXxx(t *testing.T) {
    ?????????? ??? t.Error ????????
  }
2.??????????????????
  func BenchmarkXxx(b *testing.B) {
    for i := 0; i < b.N; i++ {
      ???????????????N??
    }
  }
3.??? -- ??????????
  func ExampleXxx() {
    ??????????????????????????????????? Output ??????????????????????????б??
    //Output: ??????
  }

??????????????????????????????????????????????????????

??????????????????(Slice)???????????п???????????????Щ???????????磺??????????????????????????·??????????(?????? std????vector) ??

func TestSlice(t *testing.T) {
 //????1?????????????????10????????slice
 intSlice := make([]int?? 10?? 15) //{1?? 2?? 3?? 4?? 5?? 6?? 7?? 8?? 9?? 10}
 for idx := 0; idx < len(intSlice); idx++ {
  intSlice[idx] = idx + 1
 }
 GOUNIT_ASSERT(t?? len(intSlice) == 10?? "make??????slice????")
 GOUNIT_ASSERT(t?? cap(intSlice) == 15?? "make??????slice????")

 //??β???????????????????
 newIntSlice := append(intSlice?? 11?? 12?? 13)
 GOUNIT_ASSERT(t?? len(intSlice) == 10?? "append???????Slice????")
 GOUNIT_ASSERT(t?? len(newIntSlice) == 13?? "append???μ?Slice")
 GOUNIT_ASSERT(t?? cap(newIntSlice) == 15?? "cap(newIntSlice) == 15")
 GOUNIT_ASSERT(t?? &newIntSlice != &intSlice?? "append?????μ????")
 GOUNIT_ASSERT(t?? &newIntSlice[0] == &intSlice[0]?? "??????????????")

 newIntSlice[0] = 99
 GOUNIT_ASSERT(t?? newIntSlice[0] == intSlice[0]?? "δ????????????????????Slice??????????????")

 newAddressIntSlice := append(intSlice?? 11?? 12?? 13?? 14?? 15?? 16?? 17?? 18?? 19?? 20)
 GOUNIT_ASSERT(t?? cap(newAddressIntSlice) == 30?? "??????????????????????????????")
 newAddressIntSlice[0] = 199
 GOUNIT_ASSERT(t?? &newAddressIntSlice[0] != &intSlice[0]?? "???????????????????鯔?????")
 GOUNIT_ASSERT(t?? newAddressIntSlice[0] != intSlice[0]?? "???????????????????鯔?????")

 //????2:????????array?????????array????slice
 intArray := [10]int{0?? 1?? 2?? 3?? 4?? 5?? 6?? 7?? 8?? 9}
 intSliceFromArray := intArray[0:5] //[:]--??????????? [5:]--???5????????????????
 GOUNIT_ASSERT(t?? len(intSliceFromArray) == 5?? "array[n:m]??????slice????")
 GOUNIT_ASSERT(t?? cap(intSliceFromArray) == 10?? "array[n:m]??????slice????")
 GOUNIT_ASSERT(t?? cap(intArray) == 10?? "?????????")

 //??????????????????"index out of range"????(panic) -- ?????slice??????5(??0~4)
 //intSliceFromArray[5] = 10

 //??slice?????slice?????????????
 S3 := append(newIntSlice?? intSliceFromArray...)
 GOUNIT_ASSERT(t?? len(S3) == 18 && cap(S3) == 30?? "??slice?????slice")

 //copy???????????????????????????С????????????????и???
 var newS = make([]int?? 3)
 nCopy := copy(newS?? intSlice[0:5])
 GOUNIT_ASSERT(t?? nCopy == 3?? "?????3?????")
}