在golang concat多片

Anu*_*dha 20 go

我正在尝试合并多个切片,如下所示,

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...
    )
}
Run Code Online (Sandbox Code Playgroud)

但是内置的append()能够追加两个切片,因此它会在编译时抛出太多参数来追加.是否有替代功能来完成任务?或者有更好的方法来合并切片?

Jim*_*imB 23

append在单个元素上运行,而不是在整个切片上运行 将每个切片附加到循环中

routes := []Routes{
    WifiUserRoutes,
    WifiUsageRoutes,
    WifiLocationRoutes,
    DashboardUserRoutes,
    DashoardAppRoutes,
    RadiusRoutes,
    AuthenticationRoutes,
}

var ApplicationRoutes []Route
for _, r := range routes {
    ApplicationRoutes = append(ApplicationRoutes, r...)
}
Run Code Online (Sandbox Code Playgroud)


Cam*_*arr 23

这个问题已经回答了,但我想在此发布,因为接受的答案并不是最有效的.

原因是创建一个空切片然后追加可能会导致许多不必要的分配.

最有效的方法是预先分配切片并将元素复制到切片中.下面是一个以两种方式实现串联的包.如果您进行基准测试,您可以看到预分配速度提高了约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
Run Code Online (Sandbox Code Playgroud)

包装结束:

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
}
Run Code Online (Sandbox Code Playgroud)

基准测试:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • https://blog.golang.org/slices 似乎也支持这是更有效的解决方案。 (2认同)