master
ZGGSONG 3 years ago
parent 69f221cc7a
commit 506221df0c

@ -0,0 +1,59 @@
package main
import (
"fmt"
"reflect"
"strings"
"testing"
)
func TestArray(t *testing.T) {
fmt.Println("123")
}
func TestSum(t *testing.T) {
x := [...]int{1, 2, 3}
sum := 0
for _, v := range x {
sum += v
}
fmt.Println(sum)
}
//Newsplit 切割字符串
//example:
//abc,b=>[ac]
func Newsplit(str, sep string) (des []string) {
index := strings.Index(str, sep)
for index > -1 {
sectionBefor := str[:index]
des = append(des, sectionBefor)
str = str[index+1:]
index = strings.Index(str, sep)
}
//最后1
des = append(des, str)
return
}
//测试用例1以字符分割
func TestSplit(t *testing.T) {
got := Newsplit("123N456", "N")
want := []string{"123", "456"}
//DeepEqual比较底层数组
if !reflect.DeepEqual(got, want) {
//如果got和want不一致说明你写得代码有问题
t.Errorf("The values of %v is not %v\n", got, want)
}
}
//测试用例2以标点符号分割
func TestPunctuationSplit(t *testing.T) {
got := Newsplit("a:b:c", ":")
want := []string{"a", "b", "c"}
if !reflect.DeepEqual(got, want) {
t.FailNow() //出错就stop别往下测了
}
}

@ -0,0 +1,26 @@
package main
import "fmt"
func main() {
var a1 [3]bool
var a2 = [...]int{1, 2, 3}
fmt.Printf("a1: %T\n", a1)
fmt.Println(a2)
a3 := [...]int{1: 1, 4: 2}
fmt.Println(a3)
a := [3][2]string{
{"北京", "上海"},
{"广州", "深圳"},
{"成都", "重庆"},
}
for _, v1 := range a {
for _, v2 := range v1 {
fmt.Printf("%s\t", v2)
}
fmt.Println()
}
}

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println("hello")
}

@ -0,0 +1,81 @@
package main
import (
"fmt"
"sort"
)
func main() {
var a []string
b := []int{}
c := []bool{true, false}
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(a == nil)
fmt.Println("===切片===")
test := []int{5, 4, 3, 8, 6}
testSlice1 := test[1:3]
fmt.Printf("slice1:%v, len: %v, caps: %v\n", testSlice1, len(testSlice1), cap(testSlice1))
//切片再切片的结果起始位置在上一个切片的起始位置high上限未原始数组的容量
//我的理解a [1,2,3,4,5,6]
// 切片b=a[2,4] = [3,4] length=4-2=2 capacity=数组[3,4,5,6]的容量=4
testSlice2 := testSlice1[0:4]
fmt.Printf("slice2:%v, len: %v, caps: %v\n", testSlice2, len(testSlice2), cap(testSlice2))
//capacity=max-low=3 length=high-low=2
testSlice3 := test[2:4:5]
fmt.Printf("slice3:%v, len:%v, cap:%v\n", testSlice3, len(testSlice3), cap(testSlice3))
fmt.Println("===make===")
sliceMake := make([]bool, 2, 10)
fmt.Printf("make: %v, len: %v, cap: %v\n", sliceMake, len(sliceMake), cap(sliceMake))
fmt.Println("===赋值===")
s1 := make([]int, 3) //[0 0 0]
s2 := s1 //将s1直接赋值给s2s1和s2共用一个底层数组
s2[0] = 100
fmt.Println(s1) //[100 0 0]
fmt.Println(s2) //[100 0 0]
s11 := []int{1, 3, 5, 7, 9, 0, 2, 4, 6, 8}
s12 := make([]int, 5, 5)
copy(s12, s11) //数据复制到另一片空间
s11[0] = 100
fmt.Println(s11)
fmt.Println(s12)
fmt.Println("===遍历===")
slice4range := []int{1, 3, 5, 6, 4, 2}
for i := 0; i < len(slice4range); i++ {
fmt.Printf("%v ", slice4range[i])
}
fmt.Println()
for index, value := range slice4range {
fmt.Println(index, value)
}
fmt.Println("===删除===")
sliceDel := []int{1, 2, 3, 4, 5, 6}
sliceDel = append(sliceDel[:2], sliceDel[3:]...)
fmt.Println(sliceDel)
var m = make([]string, 5, 10)
for i := 0; i < 10; i++ {
m = append(m, fmt.Sprintf("%v", i))
}
fmt.Println(m)
var mm = [...]int{3, 7, 8, 9, 1}
ms := mm[0:len(mm)]
sort.Slice(ms, func(i, j int) bool {
return ms[i] < ms[j]
})
fmt.Printf("Sort: %v\n%v\n", mm, ms)
}
Loading…
Cancel
Save