github.com/searKing/golang/go@v1.2.117/net/addr.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package net 6 7 import ( 8 "net" 9 "strings" 10 ) 11 12 var ( 13 _ net.Addr = strAddr("") 14 _ net.Addr = multiAddrs{} 15 ) 16 17 // strAddr is a net.Addr backed by either a TCP "ip:port" string, or 18 // the empty string if unknown. 19 type strAddr string 20 21 func (a strAddr) Network() string { 22 if a != "" { 23 // Per the documentation on net/http.Request.RemoteAddr, if this is 24 // set, it's set to the IP:port of the peer (hence, TCP): 25 // https://golang.org/pkg/net/http/#Request 26 // 27 // If we want to support Unix sockets later, we can 28 // add our own grpc-specific convention within the 29 // grpc codebase to set RemoteAddr to a different 30 // format, or probably better: we can attach it to the 31 // context and use that from serverHandlerTransport.RemoteAddr. 32 return "tcp" 33 } 34 return "" 35 } 36 37 func (a strAddr) String() string { return string(a) } 38 39 type multiAddrs []net.Listener 40 41 // Network returns the address's network name, "tcp,udp". 42 func (ls multiAddrs) Network() string { 43 var networkStrs []string 44 for _, ln := range ls { 45 networkStrs = append(networkStrs, ln.Addr().Network()) 46 } 47 return strings.Join(networkStrs, ",") 48 } 49 50 func (ls multiAddrs) String() string { 51 var addrStrs []string 52 for _, l := range ls { 53 addrStrs = append(addrStrs, l.Addr().String()) 54 } 55 return strings.Join(addrStrs, ",") 56 }