func TestType(t *testing.T) { var x interface{} = 100 switch x.(type) { case nil: t.Log("type is nil") case int: t.Log("type is int") case string: t.Log("type is string") default: t.Log("other type") } }
func TestType(t *testing.T) { var x interface{} = 100 switch v := x.(type) { case nil: t.Log("type is nil") case int: t.Log("type is int", v) case string: t.Log("type is string") default: t.Log("other type") } }
这里的 v := x.(type),v得到的就是具体的值,不是x的类型。这点千万注意。这种写法也是诡异。
slc2 := month[3:6] t.Log(slc2) // [4月 May Jun] t.Log(len(slc2), cap(slc2)) // 3 9 slc2[1] = "5月" t.Log(month) // [Jan Feb Mar 4月 5月 Jun Jul Aug Sep Oct Nov Dec] t.Log(slc) // [4月 5月 Jun]
func TestArr2SliceOut(t *testing.T) { month := [12]string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} slc := month[3:6:9] t.Log(slc) // [Apr May Jun] t.Log(len(slc), cap(slc)) // 3 6 slc = append(slc, "7月") t.Log(slc) // [Apr May Jun 7月] t.Log(month) // [Jan Feb Mar Apr May Jun 7月 Aug Sep Oct Nov Dec] slc = append(slc, "8月") slc = append(slc, "9月") slc = append(slc, "10月") t.Log(slc) // [Apr May Jun 7月 8月 9月 10月] t.Log(month)// [Jan Feb Mar Apr May Jun 7月 8月 9月 Oct Nov Dec] }