rawid

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ID

type ID []byte

ID is the type of the returned value as a rawid. It contains methods to convert the rawid to other types.

func NewBase62

func NewBase62(base62string string) (ID, error)

NewBase62 returns new rawid.ID object from base62 encoded string.

Example
package main

import (
	"fmt"
	"log"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	input := "lYGhA16ahyf" // Max value in Base62

	rawID, err := rawid.NewBase62(input)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Base10:", rawID.Dec())
	fmt.Println("Base16:", rawID.Hex())
	fmt.Println("Base62:", rawID.Base62())

}
Output:

Base10: -1
Base16: ffffffffffffffff
Base62: lYGhA16ahyf

func (ID) Base62

func (r ID) Base62() string

Base62 returns the rawid as a base62 encoded string. The characters used are "0-9, a-z, A-Z".

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	// the ID must be 8 byte length
	id := rawid.ID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

	fmt.Println("Base10:", id.Dec())
	fmt.Println("Base62:", id.Base62())

}
Output:

Base10: -1
Base62: lYGhA16ahyf

func (ID) Byte

func (r ID) Byte() []byte

Byte returns the rawid as a byte slice.

Example

To get the rawid as a slice of bytes, use the ID.Bytes() method.

package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	// 0x61 = 0d97, 0x62 = 0d98, ... ... , 0x68 = 0d104
	id := rawid.ID{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68}

	fmt.Println("Byte silice:", id.Byte())

}
Output:

Byte silice: [97 98 99 100 101 102 103 104]

func (ID) Dec

func (r ID) Dec() string

Dec returns the rawid as a signed decimal string. It is the most suitable type to use in SQLite3's rawid.

Example

To get the rawid as signed decimal string, use the RawID.Dec() method.

Note that numbers are treated unsigned internally and the output is a signed number. Check the RawID.UDec method example as well.

package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	idMin := rawid.ID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
	idMid := rawid.ID{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
	idMax := rawid.ID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

	// Signed decimal
	fmt.Println("Dec string (min):", idMin.Dec())
	fmt.Println("Dec string (mid):", idMid.Dec())
	fmt.Println("Dec string (max):", idMax.Dec())

}
Output:

Dec string (min): 0
Dec string (mid): -9223372036854775808
Dec string (max): -1
Example (And_UDec)

To get the rawid as an unsigned decimal string, use the ID.UDec() method. Check the ID.Dec method example as well.

package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	idMin := rawid.ID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
	idMid := rawid.ID{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
	idMax := rawid.ID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

	// Unsigned decimal
	fmt.Println("UDec string (min):", idMin.UDec())
	fmt.Println("UDec string (mid):", idMid.UDec())
	fmt.Println("UDec string (max):", idMax.UDec())

}
Output:

UDec string (min): 0
UDec string (mid): 9223372036854775808
UDec string (max): 18446744073709551615

func (ID) Hex

func (r ID) Hex() string

Hex returns the rawid as a hex string.

Example

To get the rawid as 16 digit hexadecimal string, use the RawID.Hex() method.

Note that the first zeros are not omitted and the string is always 16 digits.

package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	idMin := rawid.ID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
	idMid := rawid.ID{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
	idMax := rawid.ID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

	fmt.Println("Hex string (min):", idMin.Hex())
	fmt.Println("Hex string (mid):", idMid.Hex())
	fmt.Println("Hex string (max):", idMax.Hex())

}
Output:

Hex string (min): 0000000000000000
Hex string (mid): 8000000000000000
Hex string (max): ffffffffffffffff

func (ID) Int64

func (r ID) Int64() int64

Int64 returns the rawid as a signed 64 bit integer.

Example

To get the rawid as an Int64 type, use the RawID.Int64() method. Check the RawID.UInt64 method example as well.

package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	id := rawid.ID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

	// Signed int64
	fmt.Printf("Type: %T, Value: %v\n", id.Int64(), id.Int64())

}
Output:

Type: int64, Value: -1

func (ID) String

func (r ID) String() string

String returns the rawid as a string. It is useless for rawid.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	id := rawid.ID{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68}

	// Type ID implements the Stringer interface. Note that both of the
	// below are equivalent.
	fmt.Println("Stringer:", id)
	fmt.Println("String  :", id.String())

}
Output:

Stringer: abcdefgh
String  : abcdefgh

func (ID) UDec

func (r ID) UDec() string

UDec returns the rawid as an unsigned decimal string (No plus or minus).

func (ID) UInt64

func (r ID) UInt64() uint64

UInt64 returns the rawid as an unsigned 64 bit integer.

Example

To get the rawid as an UInt64 type, use the RawID.UInt64() method. Check the RawID.Int64 method example as well.

package main

import (
	"fmt"

	"github.com/KEINOS/go-genrawid/pkg/rawid"
)

func main() {
	id := rawid.ID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

	// Unsigned int64
	fmt.Printf("Type: %T, Value: %v\n", id.UInt64(), id.UInt64())

}
Output:

Type: uint64, Value: 18446744073709551615

Jump to

Keyboard shortcuts

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