10 examples of 'golang concat slices' in Go

Every line of 'golang concat slices' 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.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
112func Concat(xs interface{}) interface{} {
113 chk := ty.Check(
114 new(func([][]ty.A) []ty.A),
115 xs)
116 vxs, tflat := chk.Args[0], chk.Returns[0]
117
118 xsLen := vxs.Len()
119 vflat := reflect.MakeSlice(tflat, 0, xsLen*3)
120 for i := 0; i < xsLen; i++ {
121 vflat = reflect.AppendSlice(vflat, vxs.Index(i))
122 }
123 return vflat.Interface()
124}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

11func 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}
415func (v *BaseKParserVisitor) VisitSlice(ctx *SliceContext) interface{} {
416 return v.VisitChildren(ctx)
417}
314func concatStringSlices(s ...[]string) (agg []string) {
315 for _, v := range s {
316 agg = append(agg, v...)
317 }
318 return
319}
41func concatStringSlices(sl1 []string, sls ...[]string) []string {
42 for _, sl := range sls {
43 for _, s := range sl {
44 sl1 = append(sl1, s)
45 }
46 }
47 return sl1
48}
14func concatByteSlices(a []byte, b []byte) []byte {
15 result := make([]byte, len(a)+len(b))
16 copy(result, a)
17 copy(result[len(a):], b)
18 return result
19}
272func ConcatByteSlices(a []byte, b []byte) []byte {
273 result := make([]byte, len(a)+len(b))
274 copy(result, a)
275 copy(result[len(a):], b)
276 return result
277}
159func Slice(list cell.I, start, end int64) cell.I {
160 length := Length(list)
161
162 if start < 0 {
163 start = length + start
164 }
165
166 if start < 0 {
167 panic("slice starts before first element")
168 } else if start > length {
169 start = length
170 }
171
172 if end <= 0 {
173 end = length + end
174 }
175
176 if end < 0 {
177 panic("slice ends before first element")
178 } else if end > length {
179 end = length
180 }
181
182 end -= start
183
184 if end < 0 {
185 panic("end of slice before start")
186 } else if end == 0 {
187 return pair.Null
188 }
189
190 for start > 0 {
191 list = pair.Cdr(list)
192
193 start--
194 }
195
196 slice := pair.Cons(pair.Car(list), pair.Null)
197
198 for c := slice; end > 1; end-- {
199 list = pair.Cdr(list)
200 n := pair.Cons(pair.Car(list), pair.Null)
201 pair.SetCdr(c, n)
202 c = n
203 }
204
205 return slice
206}
594func (ns *Namespace) Slice(args ...interface{}) interface{} {
595 if len(args) == 0 {
596 return args
597 }
598
599 return collections.Slice(args...)
600}
80func (x AstSlice) Slice(lo, hi int) AstWithSlice { x.X = x.X[lo:hi]; return x }

Related snippets