我正在尝试合并多个切片,如下所示,
package routes
import (
"net/http"
)
type Route struct {
Name string
Method string
Pattern string
Secured bool
HandlerFunc http.HandlerFunc
}
type Routes []Route
var ApplicationRoutes Routes
func init() {
ApplicationRoutes = append(
WifiUserRoutes,
WifiUsageRoutes,
WifiLocationRoutes,
DashboardUserRoutes,
DashoardAppRoutes,
RadiusRoutes,
AuthenticationRoutes...
)
}
然而,内置的append()能够追加两个切片,因此它会在编译时抛出过多的参数来追加。是否有替代功能来完成任务?或者是否有更好的方法来合并切片?
答案 0 :(得分:18)
这个问题已经得到了解答,但我想在此发布,因为接受的答案并不是最有效的。
原因是创建一个空切片然后追加可能会导致许多不必要的分配。
最有效的方法是预先分配切片并将元素复制到切片中。下面是一个以两种方式实现串联的包。如果您进行基准测试,您可以看到预分配速度提高了约2倍,并且分配的内存更少。
基准测试结果:
go test . -bench=. -benchmem
testing: warning: no tests to run
BenchmarkConcatCopyPreAllocate-8 30000000 47.9 ns/op 64 B/op 1 allocs/op
BenchmarkConcatAppend-8 20000000 107 ns/op 112 B/op 3 allocs/op
包concat:
package concat
func concatCopyPreAllocate(slices [][]byte) []byte {
var totalLen int
for _, s := range slices {
totalLen += len(s)
}
tmp := make([]byte, totalLen)
var i int
for _, s := range slices {
i += copy(tmp[i:], s)
}
return tmp
}
func concatAppend(slices [][]byte) []byte {
var tmp []byte
for _, s := range slices {
tmp = append(tmp, s...)
}
return tmp
}
基准测试:
package concat
import "testing"
var slices = [][]byte{
[]byte("my first slice"),
[]byte("second slice"),
[]byte("third slice"),
[]byte("fourth slice"),
[]byte("fifth slice"),
}
var B []byte
func BenchmarkConcatCopyPreAllocate(b *testing.B) {
for n := 0; n < b.N; n++ {
B = concatCopyPreAllocate(slices)
}
}
func BenchmarkConcatAppend(b *testing.B) {
for n := 0; n < b.N; n++ {
B = concatAppend(slices)
}
}
答案 1 :(得分:16)
append
对单个元素进行操作,而不是对整个切片进行操作。将每个切片附加到循环中
routes := []Routes{
WifiUserRoutes,
WifiUsageRoutes,
WifiLocationRoutes,
DashboardUserRoutes,
DashoardAppRoutes,
RadiusRoutes,
AuthenticationRoutes,
}
var ApplicationRoutes []Route
for _, r := range routes {
ApplicationRoutes = append(ApplicationRoutes, r...)
}