去:数组和地图必须是不同的概念/功能? [英] Go: Do arrays and maps have to be different concepts/features?

查看:95
本文介绍了去:数组和地图必须是不同的概念/功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中的数组可以同时使用数字键和字符串键。

例如:

  $ array [0] =我的价值。; 

  $ array ['key'] =我的价值; 

为什么不去执行这样的数组?



在Go中有两个不同的概念和语法(地图)有什么好处?



我相信我' m没有看到背后的用处。 Go不是PHP。虽然一些高级语言分享了这种抽象,但它并不常见。数组和地图是用于不同目的的不同数据结构。

PHP的数组实际上是下面的哈希表。 Go拥有真正的数组,它具有对数组更强大的抽象。

拥有真正的数组,为您提供可预测的内存布局,以及真正的O(1)索引(对于内部使用数组的Go切片也是如此)。对底层数据存储使用哈希映射会导致所有操作的开销不断增加,并且无法更好地控制数据本地。


Arrays in PHP work both with numeric keys and string keys. Which is awesome.

Ex:

$array[0] = "My value.";

or

$array['key'] = "My value";

Why doesn't go implement arrays like this?

What's the benefit for having two different concepts and syntax (maps) in Go?

I believe I'm failing to see the usefulness behind this.

解决方案

Go is not PHP. While a few higher-level languages share this abstraction, it's not very common. Arrays and Maps are different data structures for different purposes.

PHP's arrays are actually hash tables underneath. Go has true arrays, and it has slices which are a more powerful abstraction over arrays.

Having real arrays, gives you predictable memory layouts, and true O(1) indexing (the same goes for Go's slices, which use an array internally). Using a hash-map for the underlying data store costs you a constant overhead for all operations, as well as not being able to better control data locality.

这篇关于去:数组和地图必须是不同的概念/功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆