Every line of 'golang clear slice' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.
24 func Slice(arr []byte, count int) [][]byte { 25 26 sliceCount := len(arr) / count 27 result := make([][]byte, sliceCount) 28 29 for i := 0; i < sliceCount; i++ { 30 start := i * count 31 end := i*count + count 32 33 result[i] = arr[start:end] 34 } 35 36 return result 37 }
80 func (x AstSlice) Slice(lo, hi int) AstWithSlice { x.X = x.X[lo:hi]; return x }
221 func (r *Repo) clearSliceIfPossible(dest interface{}) { 222 v := reflect.ValueOf(dest) 223 vt := v.Type() 224 225 if vt.Kind() != reflect.Ptr { 226 log.Warn("cannot clear slice: dest is not pointer") 227 return 228 } 229 230 if vt.Elem().Kind() != reflect.Slice { 231 log.Warn("cannot clear slice: dest is a pointer, but not to a slice") 232 return 233 } 234 235 reflect.Indirect(v).SetLen(0) 236 }
53 func (a *strings) Slice(start, stop int) array.BaseRef { 54 return a.StringSlice(start, stop) 55 }
103 func slice(s string, from int, to int) string { 104 from = maxInt(from, 0) 105 to = minInt(to, len(s)) 106 return s[minInt(from, to):to] 107 }
363 func (sc scopedArray) Slice(from, to int64) msg.Source { return sc.under.Slice(from, to) }
272 func reverseByteSlice(slice []byte) { 273 for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 { 274 slice[i], slice[j] = slice[j], slice[i] 275 } 276 }
415 func (v *BaseKParserVisitor) VisitSlice(ctx *SliceContext) interface{} { 416 return v.VisitChildren(ctx) 417 }
13 func (n Nodes) Slice(offset, count int) changes.Collection { 14 return n[offset : offset+count] 15 }
11 func sliceDemo2() { 12 slice1 := []int{1, 2, 3} 13 printSlice(slice1) 14 // slice1[len(slice1)] = 4 // 虽然是不定长的切片,但是一旦赋值容量就固定了,这里会报错 15 16 //指定容量 17 slice1 = make([]int, 3, 5) // 3 是长度 5 是容量 18 printSlice(slice1) 19 20 // slice1[len(slice1)] = 4 // 还是会报错,虽然容量是 5 ,但是数组长度是3,这里是以长度为准,而不是容量 21 22 //使用append来追加元素,append很常用 23 slice1 = append(slice1, 4) 24 printSlice(slice1) 25 26 slice1 = append(slice1, 5) 27 slice1 = append(slice1, 6) // 到这里长度超过了容量,容量自动翻倍为 5*2 28 printSlice(slice1) 29 30 // 上面容量自动翻倍的过程可以看作和下面一致 31 slice1 = make([]int, 3, 5) // 3 是长度 5 是容量 32 slice1 = append(slice1, 4) 33 slice1 = append(slice1, 5) 34 35 // 长度不变,容量自动翻倍为 5*2 36 slice2 := make([]int, len(slice1), (cap(slice1))*2) 37 38 /* 拷贝 slice1 的内容到 slice2 */ 39 copy(slice2, slice1) // 注意是后面的拷贝给前面 40 slice2 = append(slice2, 6) 41 printSlice(slice2) 42 43 }