Is there any string length limit in GoLang's string map key?

huangapple go评论82阅读模式
英文:

Is there any string length limit in GoLang's string map key?

问题

Go语言的字符串映射键(string map key)是否有最大长度限制?实际上,我使用的是https://github.com/OneOfOne/cmap而不是Go语言的map。

问题是,我在cmap中使用的键大约有200-4000个字符的长度,这会有问题吗?

import "github.com/kokizzu/gotro/I"
import "sync/atomic"

var CACHE_IDX int64
var CACHE_KEYS cmap.CMap

func init() {
    CACHE_KEYS = cmap.New()
}

// 将一个非常长的字符串转换为较短的字符串
func RamKey_ByQuery(query string) string {
    nkey := CACHE_KEYS.Get(query)
    if nkey != nil {
        return nkey.(string)
    }
    new_idx := atomic.AddInt64(&CACHE_IDX, 1)
    ram_key := ":" + I.ToS(new_idx) // 将整数转换为字符串
    CACHE_KEYS.Set(query, ram_key)
    return ram_key
}

以上是代码部分的内容。

英文:

Is there any maximum length of Go's string map key?
Actually I use https://github.com/OneOfOne/cmap instead of Go's map.

The question is, the key I use in that cmap is about 200-4000 characters in length, will it be a problem/gotchas?

import "github.com/kokizzu/gotro/I"
import "sync/atomic"

var CACHE_IDX int64
var CACHE_KEYS cmap.CMap

func init() {
    CACHE_KEYS = cmap.New()
}

// change a really long string to a shorter one 
func RamKey_ByQuery(query string) string {
	nkey := CACHE_KEYS.Get(query)
	if nkey != nil {
		return nkey.(string)
	}
	new_idx := atomic.AddInt64(&CACHE_IDX, 1)
	ram_key := `:` + I.ToS(new_idx) // convert integer to string
	CACHE_KEYS.Set(query, ram_key)
	return ram_key
}

答案1

得分: 14

我认为唯一的限制就是你的记忆力。

英文:

I think the only limit is your memory.

答案2

得分: 4

根据Go文档,len()函数必须与返回intstring兼容。在32位系统上,int类型通常为32位宽度,在64位系统上为64位宽度。

因此,有符号的int的范围是-2^(n -1) ~ 2^(n-1) -1。由于长度不能为负数,所以应该是0 ~ 2^(n-1) -1

参考 - https://golang.org/pkg/builtin/#len

英文:

According to the go document, len() function has to be compatible with string which is returning int. The int type is usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems.

So, the range for signed int is -2^(n -1) ~ 2^(n-1) -1. Since length can't be negative, it should be 0 ~ 2^(n-1) -1

Ref - https://golang.org/pkg/builtin/#len

huangapple
  • 本文由 发表于 2016年11月18日 18:41:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/40675029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定