为什么在Go不常使用的名单? [英] Why are lists used infrequently in Go?

查看:168
本文介绍了为什么在Go不常使用的名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新去,关于它的相当兴奋。但是,在所有的语言我已经与广泛的合作:德尔福,C#,C ++,Python的 - 列表是非常重要的,因为它们可以动态调整,而不是阵列

I'm new to Go, and quite excited about it. But, in all the languages I've worked with extensively: Delp C#, C++, Python - Lists are very important because they can be dynamically resized, as opposed to arrays.

在GoLang,的确有 list.List 结构,但我认为它非常小的文件 - 无论是在的转到通过实例或我有三个围棋书籍 - 萨默,Chisnal和Balbaert - 他们都花阵列和切片了大量的时间,然后跳到地图。在烃源$ C ​​$ C的例子我也觉得很少或根本没有使用 list.List

In GoLang, there is indeed a list.Liststruct, but I see very little documentation about it - whether in Go By Example or the three Go books that I have - Summerfield, Chisnal and Balbaert - they all spend a lot of time on arrays and slices and then skip to maps. In souce code examples I also find little or no use of list.List.

似乎也不像Python中,范围不支持列表 - 大缺点IMO。我缺少的东西吗?

It also appears that, unlike Python, Range is not supported for List - big drawback IMO. Am I missing something?

切片当然是好的,但他们仍然需要基于用硬codeD大小的阵列上。这就是列表的用武之地。有没有办法不使用硬盘codeD数组大小创造围棋阵列/片?为什么列表忽略?

Slices are certainly nice, but they still need to be based on an array with a hard coded size. That's where List comes in. Is there a way to create an array /slice in Go without a hard coded array size? Why is List ignored?

推荐答案

我在几个月前问这个问题,当我第一次开始调查开始。从那以后,我每天都在阅读有关围棋和围棋中的编码。

I asked this question a few months ago, when I first started investigating Go. Since then, every day I have been reading about Go, and coding in Go.

由于我没有得到明确的回答了这个问题(虽然我已经接受一个答案)我现在要回答它自己,根据我了解,因为我问它:

Because I did not receive a clear-cut answer to this question (although I had accepted one answer) I'm now going to answer it myself, based on what I have learned, since I asked it:

有没有办法不使用硬盘codeD创建围棋阵列/片
  数组的大小?

Is there a way to create an array /slice in Go without a hard coded array size?

是的。片不需要硬codeD数组从:

Yes. Slices do not require a hard coded array to slice from:

var sl []int = make([]int,len,cap)

这code分配片 SL ,大小 LEN ,容量的上限 - LEN 上限是可以在运行时被分配的变量

This code allocates slice sl, of size len with a capacity of cap - len and cap are variables that can be assigned at runtime.

为什么 list.List 忽略?

看来主要原因 list.List 似乎很少得到关注围棋是:

It appears the main reasons list.List seem to get little attention in Go are:


  • 正如在@Nick克雷格 - 伍德的回答进行了解释,有
    几乎没有什么能与不能做的列表来完成
    同切片,经常更有效和更清洁,更
    优雅的语法。例如,范围构造:

  • As has been explained in @Nick Craig-Wood's answer, there is virtually nothing that can be done with lists that cannot be done with slices, often more efficiently and with a cleaner, more elegant syntax. For example the range construct:

for i:=range sl {
  sl[i]=i
}

不能与列表中使用 - 需要循环一个C风格。而在
很多情况下,C ++收集风格的语法必须列出使用:
的push_back 等。

也许更重要的是, list.List 不是强类型的 - 它非常类似于Python的列表和字典,它允许在不同类型混合在一起采集。这似乎违背
要转到办法的事情。围棋是一种非常强类型语言 - 例如,隐式类型转换从未在Go允许的,甚至上溯造型从 INT 的Int64 必须是
明确。但所有list.List方法取空的接口 -
什么都可以。

Perhaps more importantly, list.List is not strongly typed - it is very similar to Python's lists and dictionaries, which allow for mixing various types together in the collection. This seems to run contrary to the Go approach to things. Go is a very strongly typed language - for example, implicit type conversions never allowed in Go, even an upCast from int to int64 must be explicit. But all the methods for list.List take empty interfaces - anything goes.

一个,我放弃了Python和感动to Go是由于以下原因:
这种在Python的类型系统的弱点,虽然Python的
声称自己是强类型(IMO它不是)。 Go的 list.List 似乎
是一种杂种的,出生的矢量&lt C ++; T> 和Python的
列表(),并且也许是围棋本身。

One of the reasons that I abandoned Python and moved to Go is because of this sort of weakness in Python's type system, although Python claims to be "strongly typed" (IMO it isn't). Go'slist.Listseems to be a sort of "mongrel", born of C++'s vector<T> and Python's List(), and is perhaps a bit out of place in Go itself.

这不会在是否在不太遥远的将来的某个时候,我们发现在Go pcated list.List德$ P $让我感到吃惊,尽管也许会依然存在,要包容那些罕见情况在那里,即使使用良好的设计实践,问题最能与持有不同类型的集合解决。或者,也许它的存在,为C家开发商的桥梁,以获得舒适与围棋他们学会切片的细微差别,这是唯一去之前,据我所知。 (在某些方面似乎切片类似于流用C ++类或Delphi,但不完全。)

It would not surprise me if at some point in the not too distant future, we find list.List deprecated in Go, although perhaps it will remain, to accommodate those rare situations where, even using good design practices, a problem can best be solved with a collection that holds various types. Or perhaps it's there to provide a "bridge" for C family developers to get comfortable with Go before they learn the nuances of slices, which are unique to Go, AFAIK. (In some respects slices seem similar to stream classes in C++ or Delp but not entirely.)

虽然从一个Delphi / C ++ / Python的背景的,在我最初接触到去,我发现 list.List 来比去的切片比较熟悉,因为我已经成为更舒适围棋,我已经回来,改变了我的全部名单切片。我还没有发现任何东西了和/或地图不允许我这样做,这样我需要使用 list.List

Although coming from a Delphi/C++/Python background, in my initial exposure to Go I found list.List to be more familiar than Go's slices, as I have become more comfortable with Go, I have gone back and changed all my lists to slices. I haven't found anything yet that slice and/or map do not allow me to do, such that I need to use list.List.

这篇关于为什么在Go不常使用的名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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