Do three dots contain multiple meanings?
来源:Golang技术栈
时间:2023-03-22 18:19:00 280浏览 收藏
有志者,事竟成!如果你在学习Golang,那么本文《Do three dots contain multiple meanings?》,就很适合你!文章讲解的知识点主要包括golang,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
问题内容
As I recognize, "..." means the length of the array in the below snippet.
var days := [...]string { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }
On the other hand, "..." means unpacking the slice y
to arguments of int in
the below snippet, as I guess. I'm not really sure about this.
x := []int{1,2,3} y := []int{4,5,6} x = append(x, y...)
Now, the difference in the two meanings makes it hard for me to understand what "..." is.
正确答案
You've noted two cases of ...
in Go. In fact, there are 3:
[...]int{1,2,3}
Evaluates at compile time to [3]int{1,2,3}
a := make([]int, 500) SomeVariadicFunc(a...)
Unpacks a
as the arguments to a function. This matches the one you missed,
the variadic definition:
func SomeVariadicFunc(a ...int)
Now the further question (from the comments on the OP) -- why can ... work semantically in all these cases? The answer is that in English (and other languages), this is known as an ellipsis. From that article
Ellipsis (plural ellipses; from the Ancient Greek: 峒斘晃晃滴瓜埼瓜�, 茅lleipsis, "omission" or "falling short") is a series of dots that usually indicates an intentional omission of a word, sentence, or whole section from a text without altering its original meaning.1 Depending on their context and placement in a sentence, ellipses can also indicate an unfinished thought, a leading statement, a slight pause, and a nervous or awkward silence.
In the array case, this matches the "omission of a word, sentence, or whole section" definition. You're omitting the size of the array and letting the compiler figure it out for you.
In the variadic cases, it uses the same meaning, but differently. It also has
hints of "an unfinished thought". We often use "..." to mean "and so on." "I'm
going to get bread, eggs, milk..." in this case "..." signifies "other things
similar to breads, eggs, and milk". The use in, e.g., append
means "an
element of this list, and all the others." This is perhaps the less
immediately intuitive usage, but to a native speaker, it makes sense. Perhaps
a more "linguistically pure" construction would have been a[0]...
or even
a[0], a[1], a[2]...
but that would cause obvious problems with empty slices
(which do work with the ...
syntax), not to mention being verbose.
In general, "..." is used to signify "many things", and in this way both uses of it make sense. Many array elements, many slice elements (albeit one is creation, and the other is calling).
I suppose the hidden question is "is this good language design?" On one hand, once you know the syntax, it makes perfect sense to most native speakers of English, so in that sense it's successful. On the other hand, there's value in not overloading symbols in this way. I probably would have chose a different symbol for array unpacking, but I can't fault them for using a symbol that was probably intuitive to the language designers. Especially since the array version isn't even used terribly often.
As mentioned, this is of no issue to the compiler, because the cases can never
overlap. You can never have [...]
also mean "unpack this", so there's no
symbol conflict.
(Aside: There is another use of it in Go I omitted, because it's not in the
language itself, but the build tool. Typing something like go test ./...
means "test this package, and all packages in subdirectories of this one". But
it should be pretty clear with my explanation of the other uses why it makes
sense here.)
今天带大家了解了golang的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
-
439 收藏
-
262 收藏
-
193 收藏
-
188 收藏
-
500 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习
-
- 甜美的睫毛膏
- 细节满满,已收藏,感谢师傅的这篇技术文章,我会继续支持!
- 2023-06-11 06:40:49
-
- 专一的树叶
- 这篇文章内容真及时,太细致了,受益颇多,已加入收藏夹了,关注楼主了!希望楼主能多写Golang相关的文章。
- 2023-04-16 23:10:12
-
- 舒心的乐曲
- 赞 👍👍,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢老哥分享文章内容!
- 2023-04-16 07:39:03
-
- 美好的帆布鞋
- 这篇文章内容太及时了,好细啊,很好,收藏了,关注师傅了!希望师傅能多写Golang相关的文章。
- 2023-03-24 15:40:46
-
- 认真的帅哥
- 赞 👍👍,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢作者大大分享博文!
- 2023-03-23 10:44:55