dongpai2468 2018-12-06 08:00
浏览 544
已采纳

如何优雅地将一个数组的一部分复制到另一个数组中或注入到另一个数组中

I have the following code which works but the point here is that I want to inject or insert one array of arbitrary length into another statically sized array extending its length:

package main

import (
    "fmt"
)

func main() {
    ffmpegArguments := []string{
        "-y",
        "-i", "invideo",
        // ffmpegAudioArguments...,
        "-c:v", "copy",
        "-strict", "experimental",
        "outvideo",
    }

    var outputArguments [12]string
    copy(outputArguments[0:3], ffmpegArguments[0:3])
    copy(outputArguments[3:7], []string{"-i", "inaudio", "-c:a", "aac"})
    copy(outputArguments[7:12], ffmpegArguments[3:8])

    fmt.Printf("%#v
", ffmpegArguments)
    fmt.Printf("%#v
", outputArguments)
}

https://play.golang.org/p/peQXkOpheK4

  • 写回答

3条回答 默认 最新

  • duanli3277 2018-12-24 02:19
    关注

    I ended up modifying the answer from @Alirus by making a copy of the array first because it appears that the first assignment points to the haystack array and subsequent steps modify the haystack slice:

    // arrayInject is a helper function written by Alirus on StackOverflow in my
    // inquiry to find a way to inject one array into another _elegantly_:
    // https://stackoverflow.com/a/53647212/776896
    func arrayInject(haystack, pile []string, at int) (result []string) {
        result = make([]string, len(haystack[:at]))
        copy(result, haystack[:at])
        result = append(result, pile...)
        result = append(result, haystack[at:]...)
    
        return result
    }

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码