package main import ( "fmt" "net" "time" ) var protocolsToTest = []string{"tcp", "tcp4", "tcp6"} func listenOn(firstProtocol, secondProtocol, port string) { fmt.Println("\nTesting dual stack listeners...\n First listener:", firstProtocol, "\nSecond listener:", secondProtocol) secondListener, e := net.Listen(secondProtocol, port) if e != nil { fmt.Println("FAILURE:", e.Error()) return } else { fmt.Println("SUCCESS: Listening on", secondListener.Addr()) time.Sleep(5 * time.Second) secondListener.Close() } } func dualStack(firstProtocol string) { for _, secondProcotol := range protocolsToTest { listenOn(firstProtocol, secondProcotol, ":http") } } func main() { for i, firstProcotolBeingTested := range protocolsToTest { fmt.Println("\n========================== RUNNING TEST #", i, "\n") firstListener, e := net.Listen(firstProcotolBeingTested, ":http") if e != nil { fmt.Println(e.Error()) return } else { fmt.Println("Successfully bound to free port. Now listening on", firstProcotolBeingTested) fmt.Println("Will run dualStack() tests shortly") time.Sleep(5 * time.Second) // delay to observe that first listener is actually listening! go dualStack(firstProcotolBeingTested) time.Sleep(15 * time.Second) // ARBITRARY SLEEP firstListener.Close() } } }