Skip to content

Commit

Permalink
add examples to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jackspirou committed Sep 15, 2024
1 parent 53dc12e commit c34162b
Showing 1 changed file with 42 additions and 80 deletions.
122 changes: 42 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,112 +24,74 @@ go get github.com/agentstation/publicid
## Usage
To use the `publicid` package, you can import it into your Go project and call the `New` or `NewLong` functions to generate a public ID.
To use the `publicid` package in your Go code, follow these steps:
```go
import (
"github.com/agentstation/publicid"
)
id, err := publicid.New()
if err != nil {
log.Fatalf("Failed to generate public ID: %v", err)
}
fmt.Println("Generated public ID:", id)
```
The `New` function generates a public ID with a length of 8 characters, while the `NewLong` function generates a public ID with a length of 12 characters.
You can also use the `Attempts` option to specify the number of attempts to generate a unique public ID.
```go
id, err := publicid.New(publicid.Attempts(5))
if err != nil {
log.Fatalf("Failed to generate public ID: %v", err)
}
fmt.Println("Generated public ID:", id)
```
<!-- gomarkdoc:embed:start -->
<!-- Code generated by gomarkdoc. DO NOT EDIT -->
# publicid
1. Import the package:
```go
import "github.com/agentstation/publicid"
```
## Index
- [func New\(opts ...Option\) \(string, error\)](<#New>)
- [func NewLong\(opts ...Option\) \(string, error\)](<#NewLong>)
- [func Validate\(id string\) error](<#Validate>)
- [func ValidateLong\(fieldName, id string\) error](<#ValidateLong>)
- [type Option](<#Option>)
- [func Attempts\(n int\) Option](<#Attempts>)
<a name="New"></a>
## func [New](<https://github.com/agentstation/publicid/blob/main/publicid.go#L31>)
```go
func New(opts ...Option) (string, error)
```
New generates a unique nanoID with a length of 8 characters and the given options.
<a name="NewLong"></a>
## func [NewLong](<https://github.com/agentstation/publicid/blob/main/publicid.go#L36>)
2. Generate a short public ID (8 characters):
```go
func NewLong(opts ...Option) (string, error)
id, err := publicid.New()
if err != nil {
log.Fatalf("Failed to generate public ID: %v", err)
}
fmt.Println("Generated short public ID:", id)
// Output: Generated short public ID: Ab3xY9pQ
```
NewLong generates a unique nanoID with a length of 12 characters and the given options.
<a name="Validate"></a>
## func [Validate](<https://github.com/agentstation/publicid/blob/main/publicid.go#L60>)
3. Generate a long public ID (12 characters):
```go
func Validate(id string) error
longID, err := publicid.NewLong()
if err != nil {
log.Fatalf("Failed to generate long public ID: %v", err)
}
fmt.Println("Generated long public ID:", longID)
// Output: Generated long public ID: 7Zt3xY9pQr5W
```
Validate checks if a given field name's public ID value is valid according to the constraints defined by package publicid.

<a name="ValidateLong"></a>
## func [ValidateLong](<https://github.com/agentstation/publicid/blob/main/publicid.go#L66>)
4. Use the `Attempts` option to specify the number of generation attempts:
```go
func ValidateLong(fieldName, id string) error
id, err := publicid.New(publicid.Attempts(5))
if err != nil {
log.Fatalf("Failed to generate public ID: %v", err)
}
fmt.Println("Generated public ID with 5 attempts:", id)
// Output: Generated public ID with 5 attempts: Kj2mN8qL
```
validateLong checks if a given field name's public ID value is valid according to the constraints defined by package publicid.
<a name="Option"></a>
## type [Option](<https://github.com/agentstation/publicid/blob/main/publicid.go#L16>)
Option is a function type for configuring ID generation.
5. Validate a short public ID:
```go
type Option func(*config)
shortID := "Ab3xY9pQ"
err := publicid.Validate(shortID)
if err != nil {
fmt.Println("Invalid short ID:", err)
} else {
fmt.Println("Valid short ID:", shortID)
}
// Output: Valid short ID: Ab3xY9pQ
```
<a name="Attempts"></a>
### func [Attempts](<https://github.com/agentstation/publicid/blob/main/publicid.go#L24>)
6. Validate a long public ID:
```go
func Attempts(n int) Option
longID := "7Zt3xY9pQr5W"
err := publicid.ValidateLong("exampleField", longID)
if err != nil {
fmt.Println("Invalid long ID:", err)
} else {
fmt.Println("Valid long ID:", longID)
}
// Output: Valid long ID: 7Zt3xY9pQr5W
```
Attempts returns an Option to set the number of attempts for ID generation.
Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)
<!-- gomarkdoc:embed:end -->
These examples demonstrate how to generate, use, and validate both short (8-character) and long (12-character) public IDs using the `publicid` package.
## Makefile
Expand Down

0 comments on commit c34162b

Please sign in to comment.