nettest

最后更新于:2022-04-02 02:46:53

[TOC] ## 语法 ### Func ``` func LocalPath() (string, error) func LoopbackInterface() (*net.Interface, error) func MulticastSource(network string, ifi *net.Interface) (net.IP, error) func NewLocalListener(network string) (net.Listener, error) func NewLocalPacketListener(network string) (net.PacketConn, error) func RoutedInterface(network string, flags net.Flags) (*net.Interface, error) func SupportsIPv4() bool func SupportsIPv6() bool func SupportsRawSocket() bool func TestConn(t *testing.T, mp MakePipe) func TestableAddress(network, address string) bool func TestableNetwork(network string) bool ``` ## 实例 ### NewLocalListener ``` // 底层就是 127.0.0.1:0 ,生成一个随机端口 listener, err := nettest.NewLocalListener("tcp") if err != nil { log.Fatal(err) } dial, err := net.Dial("tcp", listener.Addr().String()) dial.Write([]byte(`hello world`)) ```
';