🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[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`)) ```