douyo770657 2016-06-17 14:39
浏览 1582
已采纳

在golang中连接多个切片

I'm trying to merge multiple slices as follows,

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...
    )
}

However the builtin append() is capable of appending two slices, hence it throws too many arguments to append at compile time. Is there an alternative function to achieve the task? or is there a better way to merge the slices?

  • 写回答

2条回答 默认 最新

  • dongyuan9292 2016-06-17 14:46
    关注

    append operates on individual elements, not on entire slices. Append each slice in a loop

    routes := []Routes{
        WifiUserRoutes,
        WifiUsageRoutes,
        WifiLocationRoutes,
        DashboardUserRoutes,
        DashoardAppRoutes,
        RadiusRoutes,
        AuthenticationRoutes,
    }
    
    var ApplicationRoutes []Route
    for _, r := range routes {
        ApplicationRoutes = append(ApplicationRoutes, r...)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路
  • ¥15 内网办公电脑进行向日葵
  • ¥15 如何输入双曲线的参数a然后画出双曲线?我输入处理函数加上后就没有用了,不知道怎么回事去掉后双曲线可以画出来
  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术