Golang 泛型:[type T],为啥要用中括号呢。。。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
viktor123
V2EX    程序员

Golang 泛型:[type T],为啥要用中括号呢。。。

  viktor123 2020 年 8 月 4 日 4464 次点击
这是一个创建于 1993 天前的主题,其中的信息可能已经有所发展或是发生改变。

传送门: https://blog.golang.org/generics-next-step

官方示例:

package main import ( "fmt" ) // The playground now supports parentheses or square brackets (only one at // a time) for generic type and function declarations and instantiations. // By default, parentheses are expected. To switch to square brackets, // the first generic declaration in the source must use square brackets. func Print[type T](s []T) { for _, v := range s { fmt.Print(v) } } func main() { Print([]string{"Hello, ", "playground\n"}) } 

High Level Overview:

  • Functions can have an additional type parameter list introduced by the keyword type: func F(type T)(p T) { ... }.
  • These type parameters can be used by the regular parameters and in the function body.
  • Types can also have a type parameter list: type M(type T) []T.
  • Each type parameter can have an optional type constraint: func F(type T Constraint)(p T) { ... }
  • Type constraints are interface types.
  • Interface types used as type constraints can have a list of predeclared types; only types whose underlying type is - one of those types can implement the interface.
  • Using a generic function or type requires passing type arguments.
  • Type inference permits omitting the type arguments in common cases.
  • If a type parameter has a type constraint its type argument must implement the interface.
  • Generic functions may only use operations permitted by the type constraint.
22 条回复    2020-08-05 18:35:13 +08:00
cmdOptionKana
    1
cmdOptionKana  
   2020 年 8 月 4 日
盲猜因为初始方案是小括号,被说小括号太多。而小于号大于号本质上不是括号,可能会影响编译速度(或语法分析的复杂性),官方好像一开始就讨厌尖括号。所以折中一下就是中括号了。
TypeError
    2
TypeError  
   2020 年 8 月 4 日
看起来还好,能和圆括号分清就行
mind3x
    3
mind3x  
   2020 年 8 月 4 日   1
很好,迈出了走向 Scala 的第一步 [并没有]
janxin
    4
janxin  
   2020 年 8 月 4 日
因为不想用<>,因为不少额外有工作量,()有语法歧义
Fitz
    5
Fitz  
   2020 年 8 月 4 日
唉, 什么时候改成方括号了, 记得 6 月份的时候不是圆括号吗
so1n
    6
so1n  
   2020 年 8 月 4 日 via Android
好像跟 python 不用<>的理由一样
Fitz
    7
Fitz  
   2020 年 8 月 4 日
6 月份的帖子还在呢 https://v2ex.com/t/682238#r_9129287
viktor123
    8
viktor123  
OP
   2020 年 8 月 4 日
@Fitz thx bro.
aloxaf
    9
aloxaf  
   2020 年 8 月 4 日
看着比小括号舒服多了……
allenhu
    10
allenhu  
   2020 年 8 月 4 日 via Android
恕我一眼看不懂
iyear
    11
iyear  
   2020 年 8 月 4 日
比尖尖的舒服多了。。。
zhuangzhuang1988
    12
zhuangzhuang1988  
   2020 年 8 月 4 日
Scala 也是[]
<> 是留给 xml 用的
tolerance
    13
tolerance  
   2020 年 8 月 4 日
等正式发布再说
someonedeng
    14
someonedeng  
   2020 年 8 月 5 日
其实看起来还不错
yrj
    15
yrj  
   2020 年 8 月 5 日 via iPad
我记得上一稿方案是小括号,还是中括号舒服
fengjianxinghun
    17
fengjianxinghun  
   2020 年 8 月 5 日
欢迎,[]()()() 比 ()()()()至少强了一分钱。
lithbitren
    18
lithbitren  
   2020 年 8 月 5 日
比小括号要更好
fengjianxinghun
    19
fengjianxinghun  
   2020 年 8 月 5 日
```go

package main

import (
"fmt"
)


// The playground now supports parentheses or square brackets (only one at
// a time) for generic type and function declarations and instantiations.
// By default, parentheses are expected. To switch to square brackets,
// the first generic declaration in the source must use square brackets.


type primitive interface {
type string, int, uint, float32
}

func Print[type T primitive](s []T) {
for _, v := range s {
fmt.Print(v)
}
fmt.Print("\n")
}

func main() {
Print([]string{"Hello, ", "playground"})
Print([]int{1,2,3,4,5})
Print([]float32{1.0})
}


```
Mohanson
    20
Mohanson  
   2020 年 8 月 5 日   1
Golang 语法树解析的复杂度是 LR(1), 得益于其 Token 的 parser 是 context free 的. 按照 Go 的性格来说, 它不可能使用 <> , 因为一旦加上这个符号就会出现歧义, 要联系上下文去"猜"这个 Token 的含义, 复杂度会变成 LR(无穷). 括号内的数字和上下文的大小成正比.

编译速度在 Golang 看来是很重要的.
fengjianxinghun
    21
fengjianxinghun  
   2020 年 8 月 5 日
@Mohanson 这点前端 parse 的开销基本微乎其微。
Sasasu
    22
Sasasu  
   2020 年 8 月 5 日
LR(1) 并不是复杂度
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     4372 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 28ms UTC 10:12 PVG 18:12 LAX 02:12 JFK 05:12
Do have faith in what you're doing.
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86