remap

package module
v0.0.0-...-ef60f89 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2018 License: MIT Imports: 2 Imported by: 0

README

关系型Map

在写map的时候总会遇到要拿下级数据的情况,代码会写得很冗杂,而且lock来lock去的很烦,就直接写一个,虽然interface的效率不太好。

其实操作在map_test.go和index_test.go里已经有详细的代码看里面就行
package main

import (
	"log"
	"remap"
)

type student struct {
	studentID int    // 学员号
	name      string // 姓名
	sex       int8   // 性别 1男 2女
	age       int8   // 年龄
	score     int8   // 分数
}

func main() {

	var hanmeimei = student{
		studentID: 1111,
		name:      "韩梅梅",
		sex:       2,
		age:       10,
		score:     90,
	}

	var lilei = student{
		studentID: 1112,
		name:      "李雷",
		sex:       1,
		age:       10,
		score:     80,
	}

	var m remap.Map
	m.Store("test", hanmeimei) // 修改操作
	d, _ := m.Load("test")     // 读取
	log.Println(1, d)
	// 1 {1111 韩梅梅 2 10 90}

	m.Delete("test")      //删除
	d, _ = m.Load("test") // 读取
	log.Println(2, d)
	// <nil>

	// 创建索引
	m.Store("hanmeimei", hanmeimei) // 修改操作
	m.Store("lilei", lilei)         // 修改操作
	m.CreateIndex("boys", func(k, v interface{}) bool {
		if v.(student).sex == 1 {
			return true
		}
		return false
	})
	i, _ := m.Index.GetIndex("boys") // 取出索引内容
	log.Println(3, i)
	// 3 &{{true} <nil> {{0 0} 0 0 0 0} {{{0 0} 0 0 0 0} map[]} map[lilei:{1112 李雷 1 10 80}]}

	m.Index.DeleteIndex("boys") // 删除索引
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map struct {
	Index relation
	// contains filtered or unexported fields
}

Map map空结构

func (*Map) CreateIndex

func (m *Map) CreateIndex(indexName string, f func(k, v interface{}) bool) *Map

CreateIndex create index 创建索引 indexName 索引名 f( v 值 ) 返回需要做key的值,返回索引内容

func (*Map) Delete

func (m *Map) Delete(key interface{})

Delete deletes the value for a key.

func (*Map) Len

func (m *Map) Len() int

Len MapLen

func (*Map) Load

func (m *Map) Load(key interface{}) (value interface{}, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*Map) LoadOrStore

func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored. 如果值存在就直接返回值,如果不存在就设置为传入的值

func (*Map) New

func (m *Map) New()

New 初始化

func (*Map) Range

func (m *Map) Range(f func(key, value interface{}) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently, Range may reflect any mapping for that key from any point during the Range call.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*Map) Store

func (m *Map) Store(key, value interface{})

Store sets the value for a key.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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