Skip to content

Instantly share code, notes, and snippets.

@karlkfi
Created June 3, 2015 20:42
Show Gist options
  • Save karlkfi/8b4777bea3e9fe37c3b1 to your computer and use it in GitHub Desktop.
Save karlkfi/8b4777bea3e9fe37c3b1 to your computer and use it in GitHub Desktop.
Script to generate a list of ports given a range
package main
import (
"os"
"strings"
"strconv"
"fmt"
)
// go run ports.go 10-19
func main() {
input := os.Args[1]
lowHigh := strings.Split(input, "-")
if len(lowHigh) != 2 {
fmt.Fprintf(os.Stderr, "Failed to parse 1st arg: '%s'", input)
os.Exit(1)
}
low, err := strconv.Atoi(lowHigh[0])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse low port: '%s': %#v", lowHigh[0], err)
os.Exit(1)
}
high, err := strconv.Atoi(lowHigh[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse high port: '%s': %#v", lowHigh[1], err)
os.Exit(1)
}
for i := low; i < high; i++ {
fmt.Printf("\"%d:%d\", ", i, i)
}
fmt.Printf("\"%d:%d\"\n", high, high)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment