go - 使用ghemawat/stream在go中的片段上应用流序列

标签 go stream slice

假设您有一个片段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/

相关文章:

arrays - 无法对二维数组的列进行 slice "cannot use Sudoku[0:9][0] (type [9]int) as type []int in assignment"

loops - slice 范围的循环索引

python - 如何在 Python 中获取列表的最后一项?

go - 如何在 Hyperledger Fabric 代码上运行测试?

javascript - 向服务器发送二进制数据

c++ - 从文件中读取和写入 int 对

c# - 返回流是否安全?

go - 将未编码的 yaml interface{} 转换为实际结构

go - 如何为 pq 驱动程序准备一个带有动态表名的 INSERT 语句

PHP: get_headers 设置临时 stream_context