output_writer

command
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

output_writer — SDK Writer 抽象示例

演示如何在 SDK 中使用 v3 新增的 Writer 能力:

  • sdk.WithOutputFile 把结果以 JSON Lines 形式写入本地文件
  • sdk.WithWriter 注入自定义 Writer(这里实现一个简易计数器)

Writer 与 ScanCallback 可以同时启用:Engine 在每完成一个目标后, 会先把结果派发给所有 Writer,再触发回调。

运行

cd example/output_writer
go run main.go

执行结束后会在当前目录看到 results.json,每行一条 JSON 形式的 TargetResult。

关键代码

counter := &countingWriter{} // 任意实现 sdk.Writer 接口的类型

engine, _ := sdk.NewEngine(ctx,
    sdk.WithOutputFile("results.json", sdk.FormatJSON),
    sdk.WithWriter(counter),
)
defer engine.Close()

// Scan / ScanCallback / ScanBatch 完成单目标后都会派发到 Writer

内置 Writer

工厂 说明
sdk.NopWriter() 不写任何内容(默认)
sdk.NewFileWriter(path, format) 写入文件,支持 FormatTXT / FormatCSV / FormatJSON
sdk.NewSockWriter(path) 监听 Unix domain socket,向所有连接广播 JSON Lines
sdk.NewMultiWriter(writers...) 串联多个 Writer,错误用 errors.Join 收集

WithOutputFile / WithSockOutputFile / WithWriter 多次调用会被自动合并到 MultiWriter。

Documentation

Overview

output_writer 演示 sdk v3 新增的 Writer 抽象。

  • WithOutputFile 把结果写入本地 JSON Lines 文件
  • WithWriter 同时注入自定义 Writer(这里实现简易计数器)

Writer 与 Scan 回调可以并存:Engine 在每完成一个目标后会先派发到所有 Writer, 再触发 ScanCallback 的用户回调。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL