Skip to content

Commit f64535f

Browse files
committed
util: Improve readability with early return
Signed-off-by: Lucas Bremgartner <[email protected]>
1 parent 5b62065 commit f64535f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

internal/util/network.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ func CanonicalNetworkAddress(address string, defaultPort int) string {
1717
if ip != nil {
1818
// If the input address is a bare IP address, then convert it to a proper listen address
1919
// using the canonical IP with default port and wrap IPv6 addresses in square brackets.
20-
address = net.JoinHostPort(ip.String(), fmt.Sprintf("%d", defaultPort))
21-
} else {
22-
// Otherwise assume this is either a host name or a partial address (e.g `[::]`) without
23-
// a port number, so append the default port.
24-
address = fmt.Sprintf("%s:%d", address, defaultPort)
20+
return net.JoinHostPort(ip.String(), fmt.Sprintf("%d", defaultPort))
2521
}
26-
} else if port == "" && address[len(address)-1] == ':' {
22+
23+
// Otherwise assume this is either a host name or a partial address (e.g `[::]`) without
24+
// a port number, so append the default port.
25+
return fmt.Sprintf("%s:%d", address, defaultPort)
26+
}
27+
28+
if port == "" && address[len(address)-1] == ':' {
2729
// An address that ends with a trailing colon will be parsed as having an empty port.
28-
address = net.JoinHostPort(host, fmt.Sprintf("%d", defaultPort))
30+
return net.JoinHostPort(host, fmt.Sprintf("%d", defaultPort))
2931
}
3032

3133
return address

0 commit comments

Comments
 (0)