An open API service indexing awesome lists of open source software.

https://github.com/sillyhatxu/id-generator


https://github.com/sillyhatxu/id-generator

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# id-generator

## [example](https://github.com/sillyhatxu/id-generator/blob/master/generator_test.go)

## Initialize your project

```
go mod init github.com/sillyhatxu/id-generator
```

## Updating dependencies

```
go mod vendor
```

## verify dependencies

```
go mod verify
```

## remove dependencies that is not used

```
go mod tidy
```

## print dependence diagram

```
go mod graph
```

## download dependencies

```
go mod download
```

# Release Template

### Feature

* [NEW] Support for Go Modules [#17](https://github.com/sillyhatxu/convenient-utils/issues/17)

---

### Bug fix

* [FIX] Truncate Latency precision in long running request [#17](https://github.com/sillyhatxu/convenient-utils/issues/17)

## new client

```
client := NewGeneratorClient("test")
id, err := client.GeneratorGroupId("test-group")
```

## params

```
client := NewGeneratorClient(
"test",
Prefix("GT"),
GroupLength(3),
SequenceFormat("%02d"),
Instance("8"),
LifeCycle(5*time.Second),
)
id, err := client.GeneratorId()
```

## example

```
func TestGeneratorClient_GeneratorId(t *testing.T) {
client :=.NewGeneratorClient("test")
for i := 0; i < 100; i++ {
id, err := client.GeneratorId()
assert.Nil(t, err)
fmt.Println(id)
}
}
```

> output

```
15690628250001
15690628250002
15690628250003
15690628250004
15690628250005
15690628250006
15690628250007
15690628250008
15690628250009
15690628250010
```

```
func TestGeneratorClient_GeneratorGroupId(t *testing.T) {
client := NewGeneratorClient("test", Prefix("GT"), GroupLength(3), SequenceFormat("%02d"))
for i := 0; i < 100; i++ {
id, err := client.GeneratorGroupId("group")
assert.Nil(t, err)
fmt.Println(id)
}
}
```

> output

```
GT156906285601516
GT156906285602516
GT156906285603516
GT156906285604516
GT156906285605516
GT156906285606516
GT156906285607516
GT156906285608516
```

> Instance : used to prevent ID duplication when multiple instances generate ids

```
func TestGeneratorClient_GeneratorGroupIdInstance(t *testing.T) {
client := NewGeneratorClient("test", Prefix("GT"), GroupLength(3), SequenceFormat("%02d"), Instance("8"))
for i := 0; i < 100; i++ {
id, err := client.GeneratorGroupId("group")
assert.Nil(t, err)
fmt.Println(id)
}
}
```

> output

```
GT8156906575501516
GT8156906575502516
GT8156906575503516
GT8156906575504516
GT8156906575505516
GT8156906575506516
```