卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章79878本站已运行4417

Go 命令行工具:管道数据

go 命令行工具:管道数据

unix 因倡导命令应该做一件事并做好它的理念而闻名。

复杂的数据处理和转换操作通常可以使用 shell 管道运算符将命令链接在一起来执行,以便一个命令的输出成为另一个命令的输入,操作和转换数据以实现所需的结果。

例如:

# sort file names.
ls | sort

# count files.
ls -l | count -l

# print out unique file extensions.
#  1. list all files that have extensions
#  2. transform the data (discard everything but extensions)
#  3. sort the list (data must be sorted to identify duplicates)
#  4. filter out duplicates
#  5. browse the results
ls *.* | sed 's/.*.//' | sort | uniq | less

使用 go,程序员可以创建高效且高性能的命令来处理数据。我们将通过以下片段来讨论这个问题。

添加行号到输出

可用于管道操作的命令的本质是从 stdin 读取并写入 stdout。

add-line-numbers.go

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {

    // buffered input that splits input on lines.
    input := bufio.newscanner(os.stdin)

    // buffered output.
    output := bufio.newwriter(os.stdout)

    lineno := 0

    // scan until eof (no more input).
    for input.scan() {
        text := input.text()
        lineno++
        s := fmt.sprintf("%03d %sn", lineno, text)

        // it would be simpler to just use fmt.println,
        // but want to emphasize piping stdin to stdout
        // explicitly.
        // intentionally ignoring return values.
        _, _ = output.writestring(s)

    }

    // always explicitly flush remaining buffered output.
    _ = output.flush()
}

此示例从 stdin 一次读取一行,并将其写回到 stdout,每行都以行号为前缀。在这里,我们使用程序文件本身作为输入来生成编号的输出。

$ cat add-line-numbers.go | go run add-line-numbers.go
001 package main
002 
003 import (
004     "bufio"
005     "fmt"
006     "os"
007 )
008 
009 func main() {
010 
011     // buffered input that splits input on lines.
012     input := bufio.newscanner(os.stdin)
013 
014     // buffered output.
015     output := bufio.newwriter(os.stdout)
016 
017     lineno := 0
018 
019     // scan until eof (no more input).
020     for input.scan() {
021         text := input.text()
022         lineno++
023         s := fmt.sprintf("%03d %sn", lineno, text)
024 
025         // it would be simpler to just use fmt.println,
026         // but want to emphasize piping stdin to stdout
027         // explicitly.
028         // intentionally ignoring return values.
029         _, _ = output.writestring(s)
030 
031     }
032 
033     // always explicitly flush remaining buffered output.
034     _ = output.flush()
035 
036 }

base64编码输入

此示例从 stdin 一次读取一行,base64 对其进行编码,然后将其写回到 stdout。

package main

import (
    "bufio"
    "encoding/base64"
    "os"
)

func main() {

    // buffered input that splits input on lines.
    input := bufio.newscanner(os.stdin)

    // base64 encoder/writer.
    encoder := base64.newencoder(
        base64.stdencoding,
        os.stdout)

    // scan until eof (no more input).
    for input.scan() {
        bytes := input.bytes()
        _, _ = encoder.write(bytes)
        _, _ = encoder.write([]byte{'n'})
    }

    // close the encoder and ensure it flushes remaining output
    _ = encoder.close()
}

由于扫描器会分割换行符 (n) 而不返回它们,因此有必要在写入每一行后显式写入换行符。

$ cat base64-encode.go | go run base64-encode.go
cgfja2fnzsbtywlucgppbxbvcnqgkaojimj1zmlvigojimvuy29kaw5nl2jhc2u2ncikcsjvcyikkqokznvuyybtywlukckgewokcs8viej1zmzlcmvkigluchv0ihroyxqgc3bsaxrzigluchv0ig9uigxpbmvzlgojaw5wdxqgoj0gynvmaw8utmv3u2nhbm5lcihvcy5tdgrpbikkcgkvlybcyxnlnjqgrw5jb2rlci93cml0zxiucgllbmnvzgvyido9igjhc2u2nc5ozxdfbmnvzgvykaojcwjhc2u2nc5tdgrfbmnvzgluzywkcqlvcy5tdgrvdxqpcgojly8gu2nhbib1bnrpbcbft0ygkg5vig1vcmugaw5wdxqplgojzm9yigluchv0llnjyw4oksb7cgkjynl0zxmgoj0gaw5wdxquqnl0zxmokqojcv8sif8gpsblbmnvzgvylldyaxrlkgj5dgvzkqojcv8sif8gpsblbmnvzgvylldyaxrlkftdynl0zxsnxg4nfskkcx0kcgkvlybdbg9zzsb0agugzw5jb2rlcibhbmqgzw5zdxjligl0igzsdxnozxmgcmvtywluaw5nig91dhb1daojxya9igvuy29kzxiuq2xvc2uokqp9cg==

您可以通过将编码结果传输到系统 base64 命令(linux 和 macos)进行解码来确认文本是否正确编码:

$ cat base64-encode.go | go run base64-encode.go | base64 -D
package main

import (
    "bufio"
    "encoding/base64"
    "os"
)

func main() {

    // Buffered input that splits input on lines.
    input := bufio.NewScanner(os.Stdin)

    // Base64 Encoder/writer.
    encoder := base64.NewEncoder(
        base64.StdEncoding,
        os.Stdout)

    // Scan until EOF (no more input).
    for input.Scan() {
        bytes := input.Bytes()
        _, _ = encoder.Write(bytes)
        _, _ = encoder.Write([]byte{'n'})
    }

    // Close the encoder and ensure it flushes remaining output
    _ = encoder.Close()
}

这篇文章摘录自我编写的关于 go 标准库功能的简短介绍性指南,这些功能对于创建命令行工具非常有用:go for cli 应用程序和工具。

卓越飞翔博客
上一篇: 使用 JavaScript 探索生成艺术
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏