chinaid

package module
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2022 License: MIT Imports: 4 Imported by: 0

README

CHINA ID(中国大陆身份证)

Build Status Go Report Card GoDoc MIT license

校验、解析中国大陆身份证号

身份证号校验

package main
import (
    "log"
    "github.com/sleagon/chinaid"
)

func main() {
    id := chinaid.IDCard("420683199006041237")
    result := id.Valid()
    log.Println(">>>>", result)
}

身份证信息解析

package main
import (
    "log"
    "github.com/sleagon/chinaid"
)

func main() {
    id := chinaid.IDCard("420683199006041237")
    result, err := id.Decode()
    if err != nil {
        log.Println("非法身份证号")
        return
    }
    log.Println(">>>>", result)
}

结果示例

{
    "sex":       1,
    "code":      420683,
    "district":  "枣阳市",
    "city":      "襄阳市",
    "province":  "湖北省",
    "birthday":  "1990-06-04T00:00:00Z"
}

地域映射

身份证里的地域码往地域转换的映射表来自中华人民共和国民政部官网,本项目里目前用的版本是2019年4月更新的版本,后续会不定期更新。

依赖示例

go get github.com/sleagon/chinaid

dep

[[constraint]]
   name = "github.com/sleagon/chinaid"
   version = "0.3"

Documentation

Index

Constants

View Source
const (
	// Female 女
	Female = iota
	// Male 男
	Male
)
View Source
const (
	// IDCardLength 身份证长度
	IDCardLength = 18
	// IDCardModBase 取模运算的基
	IDCardModBase = 11
)

Variables

View Source
var (
	// ErrInvalidIDCardNo 身份证号不正确
	ErrInvalidIDCardNo = fmt.Errorf("身份证号不合法")
)

errors

Functions

func Age added in v0.4.3

func Age(birthDate time.Time) int

Age is shorthand for AgeAt(birthDate, time.Now()), and carries the same usage and limitations.

func AgeAt added in v0.4.3

func AgeAt(birthDate time.Time, now time.Time) int

AgeAt gets the age of an entity at a certain time.

func BenchmarkIDNum

func BenchmarkIDNum(b *testing.B)

BenchmarkIDNum 解析基准测试

func SetLocation added in v0.4.3

func SetLocation(loc *time.Location)

func Transform15To18 added in v0.4.2

func Transform15To18(idCard string) (newIdCard string, err error)

Types

type Addr

type Addr struct {
	Code     int    `json:"code"` // 这里之前是CityCode,改成三级以后,直接叫Code了
	Province string `json:"province"`
	City     string `json:"city"`
	District string `json:"district"`
}

Addr 地址信息 修复问题: https://github.com/sleagon/chinaid/issues/1 改成3级划分了

func (*Addr) InitAddr

func (addr *Addr) InitAddr(code int)

InitAddr 初始化地址信息

type IDCard

type IDCard string

IDCard 身份证号信息

func (IDCard) Decode

func (card IDCard) Decode() (IDCardDetail, error)

Decode 根据身份证号解析出具体信息

func (IDCard) Valid

func (card IDCard) Valid() bool

Valid 判断身份证号是否合法

type IDCardDetail

type IDCardDetail struct {
	Sex      int       `json:"sex"`
	Birthday time.Time `json:"birthday"`
	Addr
}

IDCardDetail 解析出来的具体信息

func (IDCardDetail) GetAge added in v0.4.3

func (t IDCardDetail) GetAge() int

Jump to

Keyboard shortcuts

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