dqcqcqwq38860 2019-08-16 11:48
浏览 1345

如何在Go中拆分子切片

I'm recently using Go to create applications. My question is this: at a certain point in the program I have a string slice:

my_slice = []string{"string1","string2","string3","string4","string5","string6"}

It contains 6 strings. Which is the best procedure (easier to write and understand) to break this slice into 3 parts, for example, and distribute the content in three sub-slices? If I have:

var my_sub_slice1 []string
var my_sub_slice2 []string
var my_sub_slice3 []string

I wish at the end of it my_sub_slice1 will contain (in his first two entries) "string1","string2"; my_sub_slice2 will contain (in his first two entries) "string3","string4"; my_sub_slice3 will contain (in his first two entries) "string5","string6". I know the question is easy but I haven't found a clean and efficient way to do this yet. Thanks a lot!

  • 写回答

1条回答

  • dream07769 2019-08-16 12:28
    关注
    my_sub_slice1 := my_slice[0:2]
    my_sub_slice2 := my_slice[2:4]
    my_sub_slice3 := my_slice[4:6]
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮