The difference between new and make in the Go language

Source: Internet
Author: User

1. New can be any type, returning a pointer: *t, allocating only memory, not initializing memory, just zeroing it
2. Make can only be used for Map,slice,chan, return an initialized (instead of 0), the value of type T

Example:

var map1 = new(map[string]string)(*map1)["china"] = "中国"    // 此处会报错:panic: assignment to entry in nil map。                                               //  因为map零值为nil,不能向为nil的map中增加数据var map2 = make(map[string]string)map2["china"] = "中国"   

The difference between new and make in the Go language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.