LoginSignup
52
47

More than 5 years have passed since last update.

Goで適当に空いてるportをListenする

Last updated at Posted at 2014-08-01

テストに使うのに、適当に空いてるportをListenしてそのアドレスを知りたい。

package main

import (
    "fmt"
    "log"
    "net"
)

func main() {
    l, err := net.Listen("tcp", "127.0.0.1:0")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(l.Addr())
}

実行結果

127.0.0.1:55007

typester氏に教えて頂きました。

52
47
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
52
47