types

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: Apache-2.0 Imports: 8 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MD5

func MD5(v interface{}) string

MD5 is hashed with MD5.

Example
package main

import (
	"fmt"

	"github.com/k-kinzal/aliases/pkg/types"
)

func main() {
	var hasher types.Hasher = types.MD5
	fmt.Println(hasher("abcde"))
	fmt.Println(hasher(struct{}{}))
}
Output:

ab56b4d92b40713acc5af89985d4b786
99914b932bd37a50b983c5e7c90ae93b

func SHA1

func SHA1(v interface{}) string

SHA1 is hashed with SHA1.

Example
package main

import (
	"fmt"

	"github.com/k-kinzal/aliases/pkg/types"
)

func main() {
	var hasher types.Hasher = types.SHA1
	fmt.Println(hasher("abcde"))
	fmt.Println(hasher(struct{}{}))
}
Output:

03de6c570bfe24bfc328ccd7ca46b76eadaf4334
bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f

func SHA256

func SHA256(v interface{}) string

SHA256 is hashed with SHA256.

Example
package main

import (
	"fmt"

	"github.com/k-kinzal/aliases/pkg/types"
)

func main() {
	var hasher types.Hasher = types.SHA256
	fmt.Println(hasher("abcde"))
	fmt.Println(hasher(struct{}{}))
}
Output:

36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0c44ca42c
44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a

Types

type Hasher

type Hasher func(v interface{}) string

Hasher is a hash function interface.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set of data structure.

func NewSet

func NewSet(hasher Hasher) *Set

NewSet creates a new Set.

Example
set := NewSet(nil)
set.Add(1)
set.Add(2)
set.Add(2)
set.Add(3)

slice := set.Slice()
fmt.Println(slice[0])
fmt.Println(slice[1])
fmt.Println(slice[2])
Output:

1
2
3

func (*Set) Add

func (set *Set) Add(i interface{})

Add values to set.

func (*Set) Slice

func (set *Set) Slice() []interface{}

Slice converts from set.

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack is the FILO data structure.

func NewStack

func NewStack(hasher Hasher) *Stack

NewStack creates a new Stack.

Example
stack := NewStack(nil)

stack.Push(1)
fmt.Println(stack.Pop())
Output:

1

func (*Stack) Pop

func (stack *Stack) Pop() interface{}

Pop get data from first.

Example
var hasher Hasher = MD5
stack := NewStack(hasher)

stack.Push(1)
stack.Push(2)
fmt.Println(stack.Pop())

stack.Push(3)
stack.Push(4)
fmt.Println(stack.Pop(), stack.Pop(), stack.Pop(), stack.Pop())
Output:

2
4 3 1 <nil>

func (*Stack) Push

func (stack *Stack) Push(v interface{})

Push adds data to last of Stack.

Example
var hasher Hasher = MD5
stack := NewStack(hasher)

stack.Push(1)
stack.Push(2)
stack.Push(3)
fmt.Println(stack.Pop(), stack.Pop(), stack.Pop())
Output:

3 2 1

func (*Stack) Slice

func (stack *Stack) Slice() []interface{}

Slice converts from Stack to Slice.

Example
var hasher Hasher = MD5
stack := NewStack(hasher)

stack.Push(1)
stack.Push(2)
stack.Push(3)
fmt.Println(stack.Slice())
Output:

[3 2 1]

type Union

type Union struct {
	// contains filtered or unexported fields
}

Union has the value of type Left or Right.

func NewUnion

func NewUnion(left reflect.Kind, right reflect.Kind, value interface{}) *Union

NewUnion returns Union

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/k-kinzal/aliases/pkg/types"
)

func main() {
	union1 := types.NewUnion(reflect.Int, reflect.String, 1)
	union2 := types.NewUnion(reflect.Int, reflect.String, "abc")
	fmt.Println(union1)
	fmt.Println(union2)
}
Output:

int(1)
string("abc")

func (*Union) Left

func (u *Union) Left() reflect.Kind

Left returns the type of Left.

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/k-kinzal/aliases/pkg/types"
)

func main() {
	union := types.NewUnion(reflect.Int, reflect.String, "abc")
	fmt.Println(union.Left())
}
Output:

int

func (*Union) Right

func (u *Union) Right() reflect.Kind

Right returns the type of Right.

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/k-kinzal/aliases/pkg/types"
)

func main() {
	union := types.NewUnion(reflect.Int, reflect.String, "abc")
	fmt.Println(union.Right())
}
Output:

string

func (*Union) String

func (u *Union) String() string

Union to String

func (*Union) Type

func (u *Union) Type() reflect.Kind

Type returns the type of Left or Right.

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/k-kinzal/aliases/pkg/types"
)

func main() {
	union := types.NewUnion(reflect.Int, reflect.String, "abc")
	switch union.Type() {
	case union.Left():
		fmt.Printf("Union is `%s`", union.Left())
	case union.Right():
		fmt.Printf("Union is `%s`", union.Right())
	}
}
Output:

Union is `string`

func (*Union) Value

func (u *Union) Value() interface{}

Value returns the value of type Left or Right. But you should extend the function returning the appropriate type without calling Value.

Example
package main

import (
	"fmt"
	"reflect"

	"github.com/k-kinzal/aliases/pkg/types"
)

func main() {
	union := types.NewUnion(reflect.Int, reflect.String, "abc")
	fmt.Println(union.Value())
}
Output:

abc

Jump to

Keyboard shortcuts

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