V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Immortal
V2EX  ›  Go 编程语言

哇靠,1.18 release!

  •  1
     
  •   Immortal · 2022-03-16 01:41:54 +08:00 · 9398 次点击
    这是一个创建于 764 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我先更为敬了🐶
    终于出来了,不容易
    第 1 条附言  ·  2022-03-17 10:29:36 +08:00
    昨天看了一天文档和资料.
    感觉 generics 就像是 interface{}加强版,暂时没感受到对平时写代码会有特别大的提升,主要觉得实际使用场景并不会很多,一个项目估计就 10-20%的代码可能会用到,可能是我理解还不够深吧?
    比起 TS(平时主要写 Go 和 Ts,所以其他语言不太懂),感觉缺少了灵活性和表达能力,看以后发展吧.
    45 条回复    2022-03-22 17:50:38 +08:00
    Carseason
        1
    Carseason  
       2022-03-16 02:13:29 +08:00
    赶紧冲
    Trim21
        2
    Trim21  
       2022-03-16 04:44:31 +08:00
    发现 goland 对泛型支持有 bug ,能正常编译的代码报错,先降回 1.17 了
    Trim21
        3
    Trim21  
       2022-03-16 04:59:56 +08:00
    Carseason
        4
    Carseason  
       2022-03-16 05:38:01 +08:00
    @Trim21 可以啊
    Trim21
        5
    Trim21  
       2022-03-16 05:46:46 +08:00
    @Carseason #4 哎,试了一下现在这个写法的确可以。想当然了。编辑了一下,现在应该能触发 goland 的 bug 了
    Trim21
        6
    Trim21  
       2022-03-16 05:48:06 +08:00
    type alias 或者 type definition 都能触发
    Carseason
        7
    Carseason  
       2022-03-16 05:52:58 +08:00
    @Trim21 sogo 我用的 vscode 你 gomod 指定 1.18 了么?
    Trim21
        8
    Trim21  
       2022-03-16 05:54:03 +08:00 via Android
    @Carseason 对,不指定 1.18 也编译不了…
    iyear
        9
    iyear  
       2022-03-16 07:35:33 +08:00
    终于发了,看着 milestone 一点点到底
    tulongtou
        10
    tulongtou  
       2022-03-16 08:19:39 +08:00
    范型也太丑了
    FakNoCNName
        11
    FakNoCNName  
       2022-03-16 08:43:15 +08:00
    本来大项目的代码就不容易阅读,这下子到处抽象更晦涩了
    darksword21
        12
    darksword21  
       2022-03-16 08:47:05 +08:00 via iPhone
    起床更
    hingbong
        13
    hingbong  
       2022-03-16 09:02:07 +08:00
    @Trim21 就是 1.18 才支持泛型的,正常,idea 2022.1eap 上没复现 bug
    eudore
        14
    eudore  
       2022-03-16 09:02:42 +08:00
    升级完成
    macscsbf
        15
    macscsbf  
       2022-03-16 09:15:21 +08:00
    goland 还有些 bug, 这里放在 goland 里会报红 但是我能编译
    type Binding[S types.Stringer, P any] interface {
    Name() S
    Bind(P, any) error
    }

    type testBinding[S types.Stringer, P *http.Request] struct {
    }

    func NewTestBinding[S types.Stringer, P *http.Request]() Binding[S, P] {
    return &testBinding[S, P]{}
    }

    func (t testBinding[S, P]) Name() S {
    return "name"
    }

    func (t testBinding[S, P]) Bind(p P, a any) error {
    return nil
    }
    macscsbf
        16
    macscsbf  
       2022-03-16 09:16:12 +08:00
    types.Stringer 是自定义的一个类型
    type Stringer interface {
    ~string
    }
    macscsbf
        17
    macscsbf  
       2022-03-16 09:18:46 +08:00
    func IndexOf[T comparable](collection []T, target T) int {
    for i, t := range collection {
    if t == target {
    return i
    }
    }
    return -1
    }
    zhs227
        18
    zhs227  
       2022-03-16 09:19:45 +08:00
    goland 一般要再等一个版本更新才能解决提示报错问题。
    rrfeng
        19
    rrfeng  
       2022-03-16 09:25:52 +08:00   ❤️ 1
    不是 gopls 的 bug 吗
    wzw
        20
    wzw  
       2022-03-16 09:32:37 +08:00 via iPhone
    @zhs227 等新版本再试试 1.18
    Hanggi
        21
    Hanggi  
       2022-03-16 09:40:58 +08:00
    感觉第三方库又要爆发一波了
    hingbong
        22
    hingbong  
       2022-03-16 09:44:08 +08:00
    以上 bug 在 2022.1 eap 均无复现,看来适配了
    Mohanson
        23
    Mohanson  
       2022-03-16 09:46:24 +08:00
    看了 release note, 我的关注点全在 fuzz 上了, generics 反而感觉没什么亮点
    yukunyi
        24
    yukunyi  
       2022-03-16 09:53:25 +08:00
    马上升级试试~~~
    xlsepiphone
        25
    xlsepiphone  
       2022-03-16 09:58:39 +08:00
    brew 还没更新,懒得自己手动下载。
    keepeye
        26
    keepeye  
       2022-03-16 10:01:57 +08:00
    从 rc1 就开始用了 并未遇到什么 bug
    yongchiu
        27
    yongchiu  
       2022-03-16 10:58:06 +08:00
    goland 怎么升级了,我的一直无法升级到最新版本
    acoldfox
        28
    acoldfox  
       2022-03-16 12:33:35 +08:00
    我们鼓励在有意义的地方使用泛型,但在生产环境中部署泛型代码时,请谨慎行事
    tairan2006
        29
    tairan2006  
       2022-03-16 14:03:36 +08:00
    有点坑,目前还不推荐在生产环境使用,估计要等 1.19
    components
        30
    components  
       2022-03-16 14:24:06 +08:00
    @yongchiu https://www.jetbrains.com/toolbox-app/
    用 jetbrains toolbox 管理你下载的 app
    xnotepad
        31
    xnotepad  
       2022-03-16 14:44:51 +08:00
    goland 目前是有问题,只能暂时换回 vscode 用用了
    MoYi123
        32
    MoYi123  
       2022-03-16 18:08:12 +08:00
    有没有大哥教一下,为什么这样写是不对的?

    package main

    import "fmt"

    func Max[T comparable](x, y T) T {
    if x > y {
    return x
    }
    return y
    }

    func main() {
    fmt.Println(Max(1, 2))
    }


    .\draft.go:6:5: invalid operation: x > y (type parameter T is not comparable with >)
    keepeye
        33
    keepeye  
       2022-03-16 18:10:37 +08:00   ❤️ 1
    @MoYi123 comparable 不能用于比较大小,你得自己定义一个类型哦。。比如:

    type NumOrdered interface {
    int | int32 | int64 | float32 | float64
    }
    BrettD
        34
    BrettD  
       2022-03-16 18:11:48 +08:00 via iPhone   ❤️ 1
    @MoYi123 comparable 是比较等于或不等于,比较大小应该是 ordered 吧
    bthulu
        35
    bthulu  
       2022-03-16 18:26:00 +08:00   ❤️ 1
    梦想破灭了, 为什么要加泛型呢, 不是说好的不加的么, 骗子
    useben
        36
    useben  
       2022-03-16 18:51:54 +08:00
    2021.3.3goland 还不支持 go workspace.....
    kongkongyzt
        37
    kongkongyzt  
       2022-03-16 19:38:14 +08:00
    @useben +1....这个特性对我还挺重要的
    tairan2006
        38
    tairan2006  
       2022-03-16 21:26:05 +08:00
    @useben 测一下 eap 支持不
    jefferycao
        39
    jefferycao  
       2022-03-16 22:35:33 +08:00
    看到有说范型丑..难道 interface 就不丑么
    Akitora
        40
    Akitora  
       2022-03-17 09:18:39 +08:00
    Goland 还会报错...
    ```go
    func Contain[T comparable](a []T, b T) bool {
    for _, t := range a {
    if t == b {
    return true
    }
    }
    return false
    }

    ```
    seakingii
        41
    seakingii  
       2022-03-17 09:46:53 +08:00
    我喜欢泛型,赶紧升级一下
    Oktfolio
        42
    Oktfolio  
       2022-03-17 09:53:53 +08:00
    @xlsepiphone homebrew 看了下,每次 go 更新大版本都要延迟很久,各种依赖 go 的包都要改版本号...
    seakingii
        43
    seakingii  
       2022-03-17 09:57:43 +08:00
    升级前,GOLAND 会提示错误,但实际能运行
    升级到 2022.1 EAP 后,不提示错误了
    wwqgtxx
        44
    wwqgtxx  
       2022-03-18 11:54:52 +08:00
    golang2021.3.4 发布了,已经不提示错误了
    useben
        45
    useben  
       2022-03-22 17:50:38 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5968 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 02:06 · PVG 10:06 · LAX 19:06 · JFK 22:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.