pubip 用于获取您公共IP地址的Go语言包

pubip 用于获取您公共IP地址的Go语言包

Go 网络编程

访问GitHub主页

共23Star

详细介绍

pubip

A simple package for getting your public IP address by several services. It's inspired by go-ipify.

GitHub License GoDoc Build Status

Introduction

In short, It validates the results from several services and returns the IP address if a valid one is found. If you have ever tried to deploy services in China, you would understand what the [fallacies of distributed computing](fallacies of distributed computing) are. Based on the assumption that the services your program depends on are not always available, it's better to have more backups services. This package gives you the public IP address from several APIs that I found.

Installation

To install pubip, simply run:

$ go get -u github.com/chyeh/pubip

This will install the latest version of the package automatically.

Usage

Here's a simple example:

package main

import (
    "fmt"
    "github.com/chyeh/pubip"
)

func main() {
    ip, err := pubip.Get()
    if err != nil {
        fmt.Println("Couldn't get my IP address:", err)
    } else {
        fmt.Println("My IP address is:", ip)
    }
}

For more details, please take a look at the GoDoc.

Error handling

It returns an error when the followings happen:

  • It fails to get at least 3 results from the services
  • The results from different services are not identical

Contributing

Just send me a PR or open an issue. Please include tests for your changes.