ip2region

package
v1.2.4-release Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2017 License: Apache-2.0, MIT Imports: 5 Imported by: 4

README

golang 实现ip地址查询

获取

go get github.com/mohong122/ip2region/binding/golang

使用


package main

import (
	"fmt"
	"github.com/mohong122/ip2region/binding/golang"
)

func main() {
	fmt.Println("err")
	region, err := ip2region.New("ip2region.db")
	defer region.Close()
	if err != nil {
		fmt.Println(err)
		return
	}
	ip, err := region.MemorySearch("127.0.0.1")
	fmt.Println(ip, err)
	ip, err = region.BinarySearch("127.0.0.1")
	fmt.Println(ip, err)
	ip, err = region.BtreeSearch("127.0.0.1")
	fmt.Println(ip, err)
}

返回对象

type IpInfo struct {
	CityId   int64
	Country  string
	Region   string
	Province string
	City     string
	ISP      string
}

性能

名称 次数 平均耗时
BenchmarkBtreeSearch-4 200000 7715 ns/op
BenchmarkMemorySearch-4 2000000 840 ns/op
BenchmarkBinarySearch-4 30000 42680 ns/op

Documentation

Index

Constants

View Source
const (
	INDEX_BLOCK_LENGTH  = 12
	TOTAL_HEADER_LENGTH = 4096
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Ip2Region

type Ip2Region struct {
	// contains filtered or unexported fields
}

func New

func New(path string) (*Ip2Region, error)

func (*Ip2Region) BinarySearch

func (this *Ip2Region) BinarySearch(ipStr string) (ipInfo IpInfo, err error)

func (*Ip2Region) BtreeSearch

func (this *Ip2Region) BtreeSearch(ipStr string) (ipInfo IpInfo, err error)

func (*Ip2Region) Close

func (this *Ip2Region) Close()

func (*Ip2Region) MemorySearch

func (this *Ip2Region) MemorySearch(ipStr string) (ipInfo IpInfo, err error)

type IpInfo

type IpInfo struct {
	CityId   int64
	Country  string
	Region   string
	Province string
	City     string
	ISP      string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL