[Go]---pointers and memory allocations

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Email: hahayacoder@gmail.com

1 The pointer in the go language and the C language in the use of almost no difference, familiar with C language should be easy to grasp, the following is the go language using the pointer code

Package Mainimport "FMT" Func Main () {    var value int = 1    //pointer to int type    var pInt *int = &value    //print related information FMT . Printf ("value =%d \ pInt =%d \ *pint =%d \ n", value, PInt, *pint)    //The value pointed to by the pointer is modified by the pointer    *pint = 222fmt. Printf ("value =%d \ pInt =%d \ *pint =%d \ n", value, PInt, *pint)    //Make pointer to another address    var m int = 123    pInt = & ; M    FMT. Printf ("value =%d \ pInt =%d \ *pint =%d \ n", value, PInt, *pint)}


2 The Go language has two mechanisms for allocating memory, namely the built-in function new and make . But new and  make do not equate, they do things that are not the same, the types applied to the same, the application scenario is not the same, but in distinguishing when to use the new and makes  But there's a simple rule.
3 The new (T) function is a built-in function that allocates memory, but unlike the work done by the built-in new functions in other languages, in the Go language,new simply zeros the memory, The memory is not initialized. so in the Go language, the work ofnew (T) is to assign a zero-value memory space to the T type and return its address, that is, to return *t. In other words,new (t) returns a 0-value pointer to the newly assigned type T
That is:var pInt *int = new (int)//*pint = nil
The 4 make (T, args) function differs from the purpose of the new (t) function. Make (t, args) is used only to create slices,map , and channel ( message pipeline ), making(T, args) return type t An initialized instance of the. new(T) returns a 0-value pointer to the type T . That is, the new function returns an uninitialized 0-value pointer to *t ,  and the Make function returns the initialized instance of T .
5 The Go language has new and  make two functions for allocating memory, and there are differences in usage, mainly because of slices,maps,channel These three types must initialize the relevant data structures before they are used. For example, a slice is a data type with three items of content, including a pointer to the data (slicing inside an array), length, and capacity, and the slice value is niluntil the three items are initialized. In other words: for slices,maps,channel, make(T, args) initializes their internal data structures and prepares them for the values they will use.
For example: Make([]int, ten, +);// assign an array of shapes, length of ten, and a slice of the first ten arrays
The following is a simple example of the difference between the new function and  the Make function in the Go language.
Package Mainimport "FMT" Func Main () {    //Use new to allocate memory for a slice but return a 0 value pointer     //followed by using make initialization unnecessarily complicates the problem so hardly uses    var P *[]int = new ([]int)//*p = nil    fmt. PRINTLN (P)        //Output &[]fmt. PRINTLN (*p)/        /output []    //Use make to allocate memory for tiles within the tile allocation is generally used this method    var v []int = make ([]int, ten)    FMT. Println (v)        //output [0 0 0 0 0 0 0 0 0 0 0]}


From the above example, when allocating memory for three types of slices,maps,channel , you should use Go 's built-in make function to complicate the problem unnecessarily. 

Related Article

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.