开源项目 `orderedmap` 使用教程

开源项目 orderedmap 使用教程

orderedmaporderedmap is a golang map where the keys keep the order that they're added. It can be de/serialized from/to JSON. It's based closely on the python collections.OrderedDict.项目地址:https://gitcode.com/gh_mirrors/ord/orderedmap

1. 项目的目录结构及介绍

orderedmap 项目的目录结构相对简单,主要包含以下文件和目录:

orderedmap/
├── .github/
│   └── workflows/
│       └── ...
├── .editorconfig
├── .gitignore
├── LICENSE
├── README.md
├── go.mod
├── go.sum
├── list.go
├── orderedmap.go
└── orderedmap_test.go
  • .github/workflows/: 包含 GitHub Actions 的工作流配置文件。
  • .editorconfig: 编辑器配置文件,用于统一代码风格。
  • .gitignore: Git 忽略文件配置。
  • LICENSE: 项目许可证文件。
  • README.md: 项目说明文档。
  • go.mod: Go 模块文件,定义项目依赖。
  • go.sum: Go 模块校验文件。
  • list.go: 列表相关实现文件。
  • orderedmap.go: 有序映射的主要实现文件。
  • orderedmap_test.go: 有序映射的测试文件。

2. 项目的启动文件介绍

orderedmap 项目的主要启动文件是 orderedmap.go。该文件定义了 OrderedMap 类型及其相关方法,包括设置、获取、删除键值对等操作。以下是 orderedmap.go 的部分代码示例:

package orderedmap

import (
	"encoding/json"
	"sort"
)

type OrderedMap struct {
	keys []string
	mp   map[string]interface{}
}

func NewOrderedMap() *OrderedMap {
	return &OrderedMap{
		keys: []string{},
		mp:   make(map[string]interface{}),
	}
}

func (o *OrderedMap) Set(key string, value interface{}) {
	if _, exists := o.mp[key]; !exists {
		o.keys = append(o.keys, key)
	}
	o.mp[key] = value
}

func (o *OrderedMap) Get(key string) (interface{}, bool) {
	val, exists := o.mp[key]
	return val, exists
}

func (o *OrderedMap) Delete(key string) {
	delete(o.mp, key)
	for i, k := range o.keys {
		if k == key {
			o.keys = append(o.keys[:i], o.keys[i+1:]...)
			break
		}
	}
}

3. 项目的配置文件介绍

orderedmap 项目没有专门的配置文件,其依赖和模块信息主要通过 go.modgo.sum 文件进行管理。以下是 go.mod 文件的内容示例:

module github.com/iancoleman/orderedmap

go 1.16

require (
	github.com/stretchr/testify v1.7.0
)
  • module: 定义了模块路径。
  • go: 指定 Go 版本。
  • require: 列出了项目依赖的其他模块及其版本。

通过这些文件,可以确保项目在不同环境中的一致性和可重复构建。

orderedmaporderedmap is a golang map where the keys keep the order that they're added. It can be de/serialized from/to JSON. It's based closely on the python collections.OrderedDict.项目地址:https://gitcode.com/gh_mirrors/ord/orderedmap

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁然眉Esmond

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值