Concat And Repeat Build Slice Collections

Build new slices with slices.Concat joining multiple sources and slices.Repeat tiling a pattern n times, leaving every input untouched.

GoData Integrity

Code

gosnippet
24 lines
package main

import (
	"fmt"
	"slices"
)

func main() {
	primary := []string{"us-east", "eu-west"}
	backup := []string{"ap-south"}

	// Concat 连接多个切片并返回新切片,入参不受影响
	combined := slices.Concat(primary, backup)
	fmt.Println("concat:", combined)

	// Repeat 平铺 n 份:固定长度的占位数据或测试夹具
	tiles := slices.Repeat([]int{0, 1}, 3)
	fmt.Println("repeat:", tiles)

	// 三个源一次连齐:Concat 接受可变参数
	all := slices.Concat(primary, backup, []string{"sa-east"})
	fmt.Println("all:", all)
	fmt.Println("inputs unchanged:", primary, backup)
}

A closer look

Count the function main as the entire surface area of Concat And Repeat Build Slice Collections.

Putting it to use

Running the file prints labeled results such as concat:, repeat:, all: — each operation’s effect is visible in the output.

Core lines

all := slices.Concat(primary, backup, []string{"sa-east"})

Interactive Tools for This Topic

FAQ

What does Concat And Repeat Build Slice Collections print when run?
Run it as a complete program — the output lines carry labels such as `concat:`, `repeat:`, `all:`.