如何在Go中拆分子切片

我最近正在使用Go创建应用程序。 我的问题是这样的:在程序的某个点上,我有一个字符串切片:</ p>

  my_slice = [] string {“ string1”,“ string2”,“ string3”,  “ string4”,“ string5”,“ string6”} 
</ code> </ pre>

它包含6个字符串。 例如,哪种方法最好(更容易编写和理解),可以将此切片分为3部分,然后将内容分发到三个子切片中?
如果我有:</ p>

 < 代码> var my_sub_slice1 []字符串
var my_sub_slice2 []字符串
var my_sub_slice3 []字符串
</ code> </ pre>

我希望在它的结尾 my_sub_slice1 </ code >将包含(在他的前两个条目中)“ string1”,“ string2” </ code>; my_sub_slice2 </ code>将包含(在他的前两个条目中)“ string3”,“ string4” </ code>; my_sub_slice3 </ code>将包含(在他的前两个条目中)“ string5”,“ string6” </ code>。
我知道问题很简单,但是我没有找到一种干净有效的方法 可以做到这一点。
非常感谢!</ p>
</ div>

展开原文

原文

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!

dqwd71332
dqwd71332 切片原始切片。游览:切片
大约一年之前 回复

1个回答



  my_sub_slice1:= my_slice [0:2] 
my_sub_slice2:= my_slice [2:4]
my_sub_slice3:= my_slice [ 4:6]
</ code> </ pre>
</ div>

展开原文

原文

my_sub_slice1 := my_slice[0:2]
my_sub_slice2 := my_slice[2:4]
my_sub_slice3 := my_slice[4:6]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐