dongshanxun6479 2014-08-13 19:11
浏览 40
已采纳

前往:数组和映射是否必须具有不同的概念/功能? [关闭]

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.

  • 写回答

2条回答 默认 最新

  • dongruan6001 2014-08-13 19:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度