docs(examples): add error handling to examples

This commit is contained in:
2021-12-17 01:25:25 +00:00
parent a86282e34d
commit 74dd8fb7e9
5 changed files with 126 additions and 51 deletions

View File

@@ -37,27 +37,27 @@ alt="GitHub issues">
</p> </p>
```go ```go
s, _ := rands.Base64(16) // => CYxqEdUB1Rzno3SyZu2g/g== s, err := rands.Base64(16) // => CYxqEdUB1Rzno3SyZu2g/g==
s, _ := rands.Base64URL(16) // => zlqw9aFqcFggbk2asn3_aQ s, err := rands.Base64URL(16) // => zlqw9aFqcFggbk2asn3_aQ
s, _ := rands.Hex(16) // => 956e2ec9e7f19ddd58bb935826926531 s, err := rands.Hex(16) // => 956e2ec9e7f19ddd58bb935826926531
s, _ := rands.Alphanumeric(16) // => Fvk1PkrmG5crgOjT s, err := rands.Alphanumeric(16) // => Fvk1PkrmG5crgOjT
s, _ := rands.Alphabetic(16) // => XEJIzcZufHkuUmRM s, err := rands.Alphabetic(16) // => XEJIzcZufHkuUmRM
s, _ := rands.Upper(16) // => UMAGAFPPNDRGLUPZ s, err := rands.Upper(16) // => UMAGAFPPNDRGLUPZ
s, _ := rands.UpperNumeric(16) // => DF0CQS0TK9CPUO3E s, err := rands.UpperNumeric(16) // => DF0CQS0TK9CPUO3E
s, _ := rands.Lower(16) // => ocsmggykzrxzfwgt s, err := rands.Lower(16) // => ocsmggykzrxzfwgt
s, _ := rands.LowerNumeric(16) // => rwlv7a1p7klqffs5 s, err := rands.LowerNumeric(16) // => rwlv7a1p7klqffs5
s, _ := rands.Numeric(16) // => 9403373143598295 s, err := rands.Numeric(16) // => 9403373143598295
s, _ := rands.String(16, "abcdefABCDEF") // => adCDCaDEdeffeDeb s, err := rands.String(16, "abcdefABCDEF") // => adCDCaDEdeffeDeb
s, _ := rands.UnicodeString(16, []rune("九七二人入八力十下三千上口土夕大")) // => 下下口九力下土夕下土八上二夕大三 s, err := rands.UnicodeString(16, []rune("九七二人入八力十下三千上口土夕大")) // => 下下口九力下土夕下土八上二夕大三
s, _ := rands.DNSLabel(16) // => z0ij9o8qkbs0ru-h s, err := rands.DNSLabel(16) // => z0ij9o8qkbs0ru-h
s, _ := rands.UUID() // => a62b8712-f238-43ba-a47e-333f5fffe785 s, err := rands.UUID() // => a62b8712-f238-43ba-a47e-333f5fffe785
n, _ := rands.Int(2147483647) // => 1334400235 n, err := rands.Int(2147483647) // => 1334400235
n, _ := rands.Int64(int64(9223372036854775807)) // => 8256935979116161233 n, err := rands.Int64(int64(9223372036854775807)) // => 8256935979116161233
b, _ := rands.Bytes(8) // => [0 220 137 243 135 204 34 63] b, err := rands.Bytes(8) // => [0 220 137 243 135 204 34 63]
``` ```
## Import ## Import

View File

@@ -2,11 +2,16 @@ package rands_test
import ( import (
"fmt" "fmt"
"log"
"github.com/jimeh/rands" "github.com/jimeh/rands"
) )
func ExampleBytes() { func ExampleBytes() {
b, _ := rands.Bytes(8) b, err := rands.Bytes(8)
fmt.Printf("%+v\n", b) // => [0 220 137 243 135 204 34 63] if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", b) // => [181 153 143 235 241 20 208 173]
} }

View File

@@ -2,16 +2,25 @@ package rands_test
import ( import (
"fmt" "fmt"
"log"
"github.com/jimeh/rands" "github.com/jimeh/rands"
) )
func ExampleInt() { func ExampleInt() {
n, _ := rands.Int(2147483647) n, err := rands.Int(2147483647)
fmt.Printf("%d\n", n) // => 1334400235 if err != nil {
log.Fatal(err)
}
fmt.Printf("%d\n", n) // => 1908357440
} }
func ExampleInt64() { func ExampleInt64() {
n, _ := rands.Int64(int64(9223372036854775807)) n, err := rands.Int64(int64(9223372036854775807))
fmt.Printf("%d\n", n) // => 8256935979116161233 if err != nil {
log.Fatal(err)
}
fmt.Printf("%d\n", n) // => 6530460062499341591
} }

View File

@@ -4,6 +4,10 @@
// //
// All functions which produce strings from a alphabet of characters uses // All functions which produce strings from a alphabet of characters uses
// rand.Int() to ensure a uniform distribution of all possible values. // rand.Int() to ensure a uniform distribution of all possible values.
//
// rands is intended for use in production code where random data generation is
// required. All functions have a error return value, which should be
// checked.
package rands package rands
import "errors" import "errors"

View File

@@ -2,76 +2,133 @@ package rands_test
import ( import (
"fmt" "fmt"
"log"
"github.com/jimeh/rands" "github.com/jimeh/rands"
) )
func ExampleBase64() { func ExampleBase64() {
s, _ := rands.Base64(16) s, err := rands.Base64(16)
fmt.Println(s) // => CYxqEdUB1Rzno3SyZu2g/g== if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => nYQLhIYTqh8oH/W4hZuXMQ==
} }
func ExampleBase64URL() { func ExampleBase64URL() {
s, _ := rands.Base64URL(16) s, err := rands.Base64URL(16)
fmt.Println(s) // => zlqw9aFqcFggbk2asn3_aQ if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => zI_zrc1l0uPT4MxncR6e5w
} }
func ExampleHex() { func ExampleHex() {
s, _ := rands.Hex(16) s, err := rands.Hex(16)
fmt.Println(s) // => 956e2ec9e7f19ddd58bb935826926531 if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => b59e8977a13f3c030bd2ea1002ec8081
} }
func ExampleAlphanumeric() { func ExampleAlphanumeric() {
s, _ := rands.Alphanumeric(16) s, err := rands.Alphanumeric(16)
fmt.Println(s) // => Fvk1PkrmG5crgOjT if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => EgPieCBO7MuWhHtj
} }
func ExampleAlphabetic() { func ExampleAlphabetic() {
s, _ := rands.Alphabetic(16) s, err := rands.Alphabetic(16)
fmt.Println(s) // => XEJIzcZufHkuUmRM if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => VzcovEqvMRBWUtQC
} }
func ExampleUpper() { func ExampleUpper() {
s, _ := rands.Upper(16) s, err := rands.Upper(16)
fmt.Println(s) // => UMAGAFPPNDRGLUPZ if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => MCZEGPWGYKNUEDCK
} }
func ExampleUpperNumeric() { func ExampleUpperNumeric() {
s, _ := rands.UpperNumeric(16) s, err := rands.UpperNumeric(16)
fmt.Println(s) // => DF0CQS0TK9CPUO3E if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => 6LLPBBUW77B26X2X
} }
func ExampleLower() { func ExampleLower() {
s, _ := rands.Lower(16) s, err := rands.Lower(16)
fmt.Println(s) // => ocsmggykzrxzfwgt if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => dhoqhrqljadsztaa
} }
func ExampleLowerNumeric() { func ExampleLowerNumeric() {
s, _ := rands.LowerNumeric(16) s, err := rands.LowerNumeric(16)
fmt.Println(s) // => rwlv7a1p7klqffs5 if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => th1z1b1d24l5h8pu
} }
func ExampleNumeric() { func ExampleNumeric() {
s, _ := rands.Numeric(16) s, err := rands.Numeric(16)
fmt.Println(s) // => 9403373143598295 if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => 3378802228987741
} }
func ExampleString() { func ExampleString() {
s, _ := rands.String(16, "abcdefABCDEF") s, err := rands.String(16, "abcdefABCDEF")
fmt.Println(s) // => adCDCaDEdeffeDeb if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => BAFffADaadeeacfa
} }
func ExampleUnicodeString() { func ExampleUnicodeString() {
s, _ := rands.UnicodeString(16, []rune("九七二人入八力十下三千上口土夕大")) s, err := rands.UnicodeString(16, []rune("九七二人入八力十下三千上口土夕大"))
fmt.Println(s) // => 下下口九力下土夕下土八上二夕大三 if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => 八三口上土土七入力夕人力下三上力
} }
func ExampleDNSLabel() { func ExampleDNSLabel() {
s, _ := rands.DNSLabel(16) s, err := rands.DNSLabel(16)
fmt.Println(s) // => z0ij9o8qkbs0ru-h if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => ab-sbh5q0gfb6sqo
} }
func ExampleUUID() { func ExampleUUID() {
s, _ := rands.UUID() s, err := rands.UUID()
fmt.Println(s) // => a62b8712-f238-43ba-a47e-333f5fffe785 if err != nil {
log.Fatal(err)
}
fmt.Println(s) // => 6a1c4f65-d5d6-4a28-aa51-eaa94fa7ad4a
} }