Skip to content

Instantly share code, notes, and snippets.

@cpu
Created November 2, 2018 14:25
Show Gist options
  • Save cpu/9e232491edb5fd7db18c2e1926ee532c to your computer and use it in GitHub Desktop.
Save cpu/9e232491edb5fd7db18c2e1926ee532c to your computer and use it in GitHub Desktop.
Tiny command line tool to encode IDN domains from stdin to ASCII using IDNA2008. Errors are printed to stderr. ASCII results are printed to stdout.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"golang.org/x/net/idna"
)
func main() {
stdinBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Error reading names from standard in: %v\n", err)
}
for _, name := range strings.Split(string(stdinBytes), "\n") {
if name == "" {
continue
}
out, err := idna.ToASCII(name)
if err != nil {
fmt.Fprintf(os.Stderr, "error converting %q to ascii: %v\n", name, err)
continue
}
fmt.Printf("%s\n", out)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment