Documentation ¶
Overview ¶
Package myipcom returns your Internet-facing IPv4 or IPv6 address, sourced from the MYIP.com API. https://www.myip.com © Ben Garrett https://github.com/bengarrett/myip
Index ¶
- Constants
- Variables
- func IPv4(ctx context.Context, cancel context.CancelFunc) (string, error)
- func IPv6(ctx context.Context, cancel context.CancelFunc) (string, error)
- func Request(ctx context.Context, cancel context.CancelFunc, url string) (string, error)
- func RequestS(ctx context.Context, cancel context.CancelFunc, url string) (string, error)
- func Valid(ipv6 bool, s string) error
- type Result
Examples ¶
Constants ¶
View Source
const (
Link = "https://api.myip.com"
)
Variables ¶
View Source
var ( ErrNoIP = errors.New("ip address is empty") ErrNoIPv4 = errors.New("ip address is not v4") ErrNoIPv6 = errors.New("ip address is not v6") ErrInvalid = errors.New("ip address is invalid") ErrRequest = errors.New("myip.com error") ErrStatus = errors.New("unusual myip.com server response") )
Functions ¶
func IPv4 ¶
IPv4 returns the clients online IP address.
Example ¶
ExampleIPv4 demonstrates an IPv4 address request with a 5 second timeout. Output: true.
package main import ( "context" "fmt" "log" "net" "time" "github.com/bengarrett/myip/pkg/myipcom" ) func main() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() s, err := myipcom.IPv4(ctx, cancel) if err != nil { log.Printf("\n%s\n", err) } fmt.Println(net.ParseIP(s) != nil) }
Output:
func IPv6 ¶
IPv6 returns the clients online IP address. Using this on a network that does not support IPv6 will result in an error.
Example ¶
ExampleIPv6 demonstrates cocurrent IPv4 and IPv6 address requests with a 5 second timeout.
package main import ( "context" "fmt" "log" "time" "github.com/bengarrett/myip/pkg/myipcom" ) func main() { ctx4, cancel4 := context.WithTimeout(context.Background(), 5*time.Second) defer cancel4() ctx6, cancel6 := context.WithTimeout(context.Background(), 5*time.Second) defer cancel6() s4 := make(chan string) s6 := make(chan string) go func() { s, err := myipcom.IPv4(ctx4, cancel4) if err != nil { log.Printf("\n%s\n", err) } s4 <- s }() go func() { s, err := myipcom.IPv6(ctx6, cancel6) if err != nil { log.Printf("\n%s\n", err) } s6 <- s }() ip4 := <-s4 ip6 := <-s6 fmt.Println(ip4) fmt.Println(ip6) }
Output:
Types ¶
Click to show internal directories.
Click to hide internal directories.