Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Go

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"os"
	"os/exec"
)

func main() {
	request, err := json.Marshal(map[string]string{
		"op": "redact", "language": "en", "text": "Contact alice@example.com",
	})
	if err != nil {
		log.Fatal(err)
	}
	cmd := exec.Command(os.Args[1])
	cmd.Stdin = bytes.NewReader(append(request, '\n'))
	output, err := cmd.Output()
	if err != nil {
		log.Fatal(err)
	}
	var response struct {
		OK     bool   `json:"ok"`
		Text   string `json:"text"`
		Reason string `json:"reason"`
	}
	if err := json.Unmarshal(output, &response); err != nil {
		log.Fatal(err)
	}
	if !response.OK {
		log.Fatal(response.Reason)
	}
	fmt.Println(response.Text) // Contact [EMAIL_1]
}

Call the JSONL subprocess interface with Go’s standard library; the Nix-built executable uses compiled model and runtime defaults.

nix build
PREFIX=$(cd result && pwd -P)
go run examples/go/main.go "$PREFIX/bin/defingerprinter"