dic

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 7 Imported by: 2

Documentation

Overview

基于map[string]interface{}定义的Dic类型,定义一些扩展的方法

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrTypeCast = errors.New("failed to type cast")
)

Functions

This section is empty.

Types

type Dic

type Dic map[string]interface{}

Dic 基于map[string]interface{}定义,更方便使用

Example
package main

import (
	"fmt"

	"github.com/recallsong/go-utils/container/dic"
)

func main() {
	dic := dic.Dic{
		"int":   123,
		"str":   "hello",
		"dic":   dic.New().Set("key", "str_val"),
		"map":   map[string]interface{}{"key": 123},
		"slice": []int{1, 2, 3},
	}
	dic["other"] = "other_values"
	dic.Set("other1", "value1").Set("other2", "value2")
	fmt.Println(dic)
	d, _ := dic.GetDic("dic")
	fmt.Println(d)
	m, _ := dic.GetDic("map")
	fmt.Println(m)
	s := dic.Get("slice")
	fmt.Println(s)
	size := dic.Size()
	fmt.Println(size)
	keys := dic.Keys()
	fmt.Println(len(keys))
}
Output:

{"dic":{"key":"str_val"},"int":123,"map":{"key":123},"other":"other_values","other1":"value1","other2":"value2","slice":[1,2,3],"str":"hello"}
{"key":"str_val"}
{"key":123}
[1 2 3]
8
8

func FromJSON

func FromJSON(jsonStr string) (Dic, error)

FromJSON 将JSON字符串解析到Dic

func FromMap

func FromMap(mp map[string]interface{}) Dic

FromMap 将map[string]interface{}转换成Dic类型

func New

func New() Dic

New 创建Dic

func (Dic) Clear

func (d Dic) Clear()

Clear 删除所有的健值对

func (Dic) Contains

func (d Dic) Contains(key string) bool

Contains 判断Dic是否包含该key

func (Dic) Copy

func (d Dic) Copy() Dic

Copy 对Dic进行浅拷贝

func (Dic) Delete

func (d Dic) Delete(key string) interface{}

Delete 删除key,并返回将要被删除的值

func (Dic) Equals

func (d Dic) Equals(obj interface{}) bool

Equals 对两个Dic进行深度比较,返回是否相等

func (Dic) Get

func (d Dic) Get(key string) interface{}

Get 根据key获取value

func (Dic) GetBool

func (d Dic) GetBool(key string, defVal bool) bool

GetBool 根据key获取bool类型的值,如果值不是int类型,将返回defVal

func (Dic) GetDic

func (d Dic) GetDic(key string) (Dic, error)

GetDic 根据key获取Dic类型的值,如果失败则返回错误

func (Dic) GetDuration

func (d Dic) GetDuration(key string, defVal time.Duration) time.Duration

GetDuration 根据key获取time.Duration类型的值,如果失败则返回defVal

func (Dic) GetFloat32

func (d Dic) GetFloat32(key string, defVal float32) float32

GetFloat32 根据key获取float32类型的值,如果值不是float32类型,将返回defVal

func (Dic) GetFloat64

func (d Dic) GetFloat64(key string, defVal float64) float64

GetFloat64 根据key获取float64类型的值,如果值不是float64类型,将返回defVal

func (Dic) GetInt

func (d Dic) GetInt(key string, defVal int) int

GetInt 根据key获取int类型的值,如果值不是int类型,将返回defVal

func (Dic) GetInt16

func (d Dic) GetInt16(key string, defVal int16) int16

GetInt16 根据key获取int16类型的值,如果值不是int16类型,将返回defVal

func (Dic) GetInt32

func (d Dic) GetInt32(key string, defVal int32) int32

GetInt32 根据key获取int32类型的值,如果值不是int32类型,将返回defVal

func (Dic) GetInt64

func (d Dic) GetInt64(key string, defVal int64) int64

GetInt64 根据key获取int64类型的值,如果值不是int64类型,将返回defVal

func (Dic) GetInt8

func (d Dic) GetInt8(key string, defVal int8) int8

GetInt8 根据key获取int8类型的值,如果值不是int8类型,将返回defVal

func (Dic) GetMap

func (d Dic) GetMap(key string) (map[string]interface{}, error)

GetDic 根据key获取map[string]interface{}类型的值,如果失败则返回错误

func (Dic) GetString

func (d Dic) GetString(key string, defVal string) string

GetStrng 根据key获取string类型的值,如果值不是string类型,将返回defVal

func (Dic) GetUint

func (d Dic) GetUint(key string, defVal uint) uint

GetUint 根据key获取uint类型的值,如果值不是uint类型,将返回defVal

func (Dic) GetUint16

func (d Dic) GetUint16(key string, defVal uint16) uint16

GetUint16 根据key获取uint16类型的值,如果值不是uint16类型,将返回defVal

func (Dic) GetUint32

func (d Dic) GetUint32(key string, defVal uint32) uint32

GetUint32 根据key获取uint32类型的值,如果值不是uint32类型,将返回defVal

func (Dic) GetUint64

func (d Dic) GetUint64(key string, defVal uint64) uint64

GetUint64 根据key获取uint64类型的值,如果值不是uint64类型,将返回defVal

func (Dic) GetUint8

func (d Dic) GetUint8(key string, defVal uint8) uint8

GetUint8 根据key获取uint8类型的值,如果值不是uint8类型,将返回defVal

func (Dic) JSON

func (d Dic) JSON() string

JSON 将Dic转换成JSON字符串,失败则返回空字符串

func (Dic) Keys

func (d Dic) Keys() []string

Keys 获取所有的key

func (Dic) LoadJSON

func (d Dic) LoadJSON(jsonStr string) error

LoadJSON 将json字符串解析到Dic,失败将返回错误

func (Dic) Put

func (d Dic) Put(key string, value interface{})

Put 设置健值对

func (Dic) Set

func (d Dic) Set(key string, value interface{}) Dic

Set 设置健值对

func (Dic) Size

func (d Dic) Size() int

Size 返回Dic的元素数量

func (Dic) String

func (d Dic) String() string

JSON 将Dic转换成JSON字符串,失败则返回空字符串

func (Dic) ToMap

func (d Dic) ToMap() map[string]interface{}

ToMap 将Dic转换成 map[string]interface{}

func (Dic) Values

func (d Dic) Values() []interface{}

Values 获取所有的值

type SyncDicPair

type SyncDicPair struct {
	Dic
	sync.RWMutex
}

SyncDicPair 带锁的Dic类型

Jump to

Keyboard shortcuts

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