From 1f96c83bf68d63180be654802f00477533bce8a5 Mon Sep 17 00:00:00 2001 From: Mikio Hara Date: Mon, 14 Mar 2016 10:02:47 +0900 Subject: [PATCH] net: simplify ipToSockaddr Change-Id: I5dbcdf0ee0b46b760b2a7decb1d937aac2a6fa8d Reviewed-on: https://go-review.googlesource.com/20585 Run-TryBot: Mikio Hara TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/net/ipsock_posix.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/net/ipsock_posix.go b/src/net/ipsock_posix.go index a42ca9c3e83..28cdb210aed 100644 --- a/src/net/ipsock_posix.go +++ b/src/net/ipsock_posix.go @@ -170,11 +170,8 @@ func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, e if ip = ip.To4(); ip == nil { return nil, &AddrError{Err: "non-IPv4 address", Addr: ip.String()} } - sa := new(syscall.SockaddrInet4) - for i := 0; i < IPv4len; i++ { - sa.Addr[i] = ip[i] - } - sa.Port = port + sa := &syscall.SockaddrInet4{Port: port} + copy(sa.Addr[:], ip) return sa, nil case syscall.AF_INET6: if len(ip) == 0 { @@ -189,12 +186,8 @@ func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, e if ip = ip.To16(); ip == nil { return nil, &AddrError{Err: "non-IPv6 address", Addr: ip.String()} } - sa := new(syscall.SockaddrInet6) - for i := 0; i < IPv6len; i++ { - sa.Addr[i] = ip[i] - } - sa.Port = port - sa.ZoneId = uint32(zoneToInt(zone)) + sa := &syscall.SockaddrInet6{Port: port, ZoneId: uint32(zoneToInt(zone))} + copy(sa.Addr[:], ip) return sa, nil } return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}