Documentation ¶
Overview ¶
Package myipcom returns your Internet-facing IPv4 or IPv6 address, sourced from the ipify API. https://www.ipify.org © 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 RequestB(ctx context.Context, cancel context.CancelFunc, url string) ([]byte, error)
- func Valid(ip string) error
Examples ¶
Constants ¶
View Source
const ( Linkv4 = "https://api.ipify.org" Linkv6 = "https://api6.ipify.org" )
Variables ¶
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/ipify" ) func main() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() s, err := ipify.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/ipify" ) 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 := ipify.IPv4(ctx4, cancel4) if err != nil { log.Printf("\n%s\n", err) } s4 <- s }() go func() { s, err := ipify.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 ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.