Golang basics and the new () keyword

I studied the golangs, and when I went through the chapter describing Structures, I came across various ways to initialize structures.

p1 := passport{} var p2 passport p3 := passport{ Photo: make([]byte, 0, 0), Name: "Scott", Surname: "Adam", DateOfBirth: "Some time", } fmt.Printf("%s\n%s\n%s\n", p1, p2, p3) 

While they print the structure values ​​as

{ } { } { Scott Adam Some time} , the following code prints with an ampersand because it is a link.

 pointerp1 := &p3 fmt.Printf("%s", pointerp1) pointerp2 := new(passport) pointerp2.Name = "Anotherscott" fmt.Printf("%s", pointerp2) 

&{ Scott Adam Some time}&{ Anotherscott }

Please help me with my doubts.

  • in using pointerp1 := &p3 , pointerp1 is a reference variable in p3 that contains the actual data. Similarly, what would be the actual variable containing the data for pointerp2 ?

  • What would be the best scenarios for using these different types of initialization?

+5
source share
3 answers

new allocates nullified storage for a new element or something, and then returns a pointer to it. I don't think it really matters if you use new vs short variable declaration := type{} , this is basically just a preference

As for pointer2 , the pointer2 variable contains its own data when you do

 // initializing a zeroed 'passport in memory' pointerp2 := new(passport) // setting the field Name to whatever pointerp2.Name = "Anotherscott" 

new allocates the nullified storage in memory and returns a pointer to it, therefore, in short, the new one returns a pointer to everything you do, therefore pointerp2 returns &{ Anotherscott }

Basically you want to use pointers when you pass a variable around that you need to change (but be careful with data races using mutexes or pipes. If you need to read and write a variable from different functions)

The usual method that people use instead of new simply shortens the type of pointer:

blah := &passport{}

blah is now a pointer to a passport type

You can see on this playground:

http://play.golang.org/p/9OuM2Kqncq

When passing a pointer, you can change the original value. When transferring a non pointer, you cannot change it. This is because go variables are passed as a copy. Thus, in the iDontTakeAPointer function iDontTakeAPointer he receives a copy of the tester structure, then modifies the name field and then returns that it does nothing for us, since it changes the copy, not the original.

+12
source
  • There is a variable that contains data. You can dereference a pointer with *pointerp2 and even assign it to a variable ( p2 := pointerp2 ), but this variable will be a copy of the data. That is, changing one does not affect the other anymore ( http://play.golang.org/p/9yRYbyvG8q ).

  • new tends to be less popular, especially with regard to textures. A good discussion of its purpose (hint: this comes first), and usage examples can be found at https://softwareengineering.stackexchange.com/a/216582 .

Edit: In addition, p1 is not really a different initialization type from p3 , but instead of assigning a value to any of the type fields, they are initialized to a zero value ( "" for string , nil for []byte ). The same will happen for any omitted fields:

 p4 := passport{ Name: "Scott", Surname: "Adam", } 

In this case, p4.Photo and p4.DateOfBirth will still be null ( nil and "" respectively). In the case of passport{} it is just one where all fields are omitted.

+1
source

The all new does keyword basically creates an instance of the type you want. However, instead of returning a simple type declaration, it refers to it and returns the acutal address of the memory of this type in the heap of the program process.

0
source

Source: https://habr.com/ru/post/1239560/


All Articles