mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-01-23 10:35:44 +00:00
29 lines
553 B
Go
29 lines
553 B
Go
package network
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Address string
|
|
|
|
func (a *Address) Port() (int, error) {
|
|
if len(string(*a)) == 0 {
|
|
return 0, errors.New("no address")
|
|
}
|
|
addr := replaceAllExceptLast(string(*a), ":", "_")
|
|
_, port, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if val, err := strconv.Atoi(port); err == nil {
|
|
return val, nil
|
|
}
|
|
return 0, errors.New("port is not a number")
|
|
}
|
|
|
|
func replaceAllExceptLast(s, c, x string) string {
|
|
return strings.Replace(s, c, x, strings.Count(s, c)-1)
|
|
}
|