go new和make的区别
发布日期:2021-05-10 07:17:32 浏览次数:12 分类:精选文章

本文共 3085 字,大约阅读时间需要 10 分钟。

Go������������������������new���make���������������������������������������allocation primitives���������������������new������������������make������slice���map������channel���������������

���������������new���make������������������������������

  • new(T)������������T������������������������������T������������������������������������������������������T���������������������������������������������������*T������������������������������������������������������������������������������������������������������

  • make(T, args)������������������������������T���������������make������������������slice���map���channel���������������������������������������������������������������������������������T���������������������������������������������������������������������������������������������������������������������������������

  • ���������������������make���������new���������������������slice���map���channel������������������������������������������������������������������������������

    slice���������

    slice���Go���������������������������������������������������������������������������������������������������������slice������������������

    // ������������������������������
    var s1 []int
    if s1 == nil {
    // s1������������
    fmt.Printf("s1 is nil --> %v \n", s1) // []int(nil)
    }
    // ������make���������slice
    s2 := make([]int, 3)
    if s2 == nil {
    // ���������������������������make���������������������������slice
    fmt.Printf("s2 is nil --> %v \n", s2) // []int(nil)
    } else {
    fmt.Printf("s2 is not nil --> %v \n", s2) // [0 0 0]
    }
    // another example
    sliceTest := make([]int, 10)
    sliceTest[0] = 42

    ������make������������slice���������������������������������������������slice������������������������������������������������������������������������nil������������������make������������������������������������������

    ������������

  • make������������������slice���map���channel������������������������������������new������������������������

  • ���������������������������make������������������������������������������������������������������������������������������������������������������������������������������������������������

  • new������������������������������������������������������������������struct���������������������������������������������������������������������������new������������������������������������������������������

  • map���������

    map���Go���������������������������������������������������������������������������������������������make������������������

    map1 := make(map[string]float32)
    // ���������������������������map������������������������������������������
    // ���������������������������������������������������

    ���new���������������������������������new������������������map���������new������������������������������������������������������map���������������������������������������������������������������

    ���������������������new���make���������������������������������������������������������������������������������������������make������������������slice���map���channel������������������������new���������������������������������������������������������������

    上一篇:Mac使用grpc时候,安装protobuf
    下一篇:Node.js 安装配置

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月17日 18时29分09秒