我有以下有效的代码,但这里的要点是,我想将任意长度的一个数组插入或插入另一个扩展其长度的静态大小的数组中:
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\n", ffmpegArguments)
fmt.Printf("%#v\n", outputArguments)
}
我最终通过首先制作数组的副本来修改@Alirus的答案,因为看起来第一个赋值指向haystack数组,随后的步骤修改了该haystack
切片:
// 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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句