假设您有一个片段items
,并且您想使用流包ghemawat/stream在此片上应用一系列流操作。怎么办呢?据我所知,输入流序列字符串输入的唯一方法是使用stream.items(items ...string)
,它不能在 slice 上使用。
最佳答案
我发现的解决方案是使用以下技巧:
我们首先使用stream.Numbers(1, len(items))
并添加一个 map 过滤器stream.Map(fn func(string) string)
,如下所示:
package main
import("strconv")
func main() {
sliceStreamExample()
}
func sliceStreamExample() {
items := []string{"foo", "bar"} // define a sample slice
stream.Run { // using Map to and Numbers to emit the slice content
stream.Numbers(1, len(items)),
stream.Map(func(num string) string {
return items[strconv.Atoi(num)]
}),
stream.WriteLines(os.Stdout)
}
}
输出:foo
bar
我不知道它是否是最佳方法。如果发现更简单的解决方案,请分享您的解决方案!谢谢。
关于go - 使用ghemawat/stream在go中的片段上应用流序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63817126/