Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang basics struct and new() keyword

I was learning golang, and as I was going through the chapter that describes Structures, I came across different 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 these print the values of the structures as

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

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

&{ Scott Adam Some time}&{ Anotherscott }

Kindly help me with my doubts.

  1. in the usage pointerp1 := &p3, pointerp1 is the reference variable to p3, which holds the actual data. Similarly, what would be the actual variable that holds the data for pointerp2?

  2. What would be the best scenarios to use these different types of initialization?

like image 804
scott Avatar asked Dec 31 '15 08:12

scott


People also ask

Can we use new keyword with struct?

A struct object can be created with or without the new operator, same as primitive type variables. Above, an object of the Coordinate structure is created using the new keyword.

Is new A keyword in Golang?

Using the keyword new to initialize a struct in Golang gives the user more control over the values of the various fields inside the struct .

How do I create a struct in Go?

It is followed by the name of the type (Address) and the keyword struct to illustrate that we're defining a struct. The struct contains a list of various fields inside the curly braces. Each field has a name and a type. The above code creates a variable of a type Address which is by default set to zero.

What does * struct mean in Golang?

A struct (short for "structure") is a collection of data fields with declared data types. Golang has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types.

How to instantiate struct using new keyword in Golang?

- GeeksforGeeks How to instantiate Struct using new keyword in Golang? A struct is mainly a holder for all other data types. By using a pointer to a struct we can easily manipulate/access the data assigned to a struct. We can instantiate Struct using the new keyword as well as using Pointer Address Operator in Golang as shown in the below example:

What is a struct in Golang?

A struct (short for "structure") is a collection of data fields with declared data types. Golang has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types. Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type.

How do you declare a data type in Golang?

Golang has the ability to declare and create own data types by combining one or more types, including both built-in and user-defined types. Each data field in a struct is declared with a known type, which could be a built-in type or another user-defined type.

How to create and initialize a struct in Go language?

Creating and initializing a Struct in GoLang 1 Using struct Literal Syntax#N#Struct literal syntax is just assigning values when declaring and it is really easy. 2 Using the new keyword#N#We can use the new keyword when declaring a struct. Then we can assign values using dot... 3 Using pointer address operator More ...


1 Answers

new allocates zeroed storage for a new item or type whatever and then returns a pointer to it. I don't think it really matters on if you use new vs short variable declaration := type{} it's mostly just preference

As for pointer2, the pointer2 variable holds 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 zeroed storage in memory and returns a pointer to it, so in short, new will return a pointer to whatever you're making that is why pointerp2 returns &{ Anotherscott }

You mainly want to use pointers when you're passing a variable around that you need to modify (but be careful of data races use mutexes or channels If you need to read and write to a variable from different functions)

A common method people use instead of new is just short dec a pointer type:

blah := &passport{}

blah is now a pointer to type passport

You can see in this playground:

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

When passing a pointer, you can modify the original value. When passing a non pointer you can't modify it. That is because in go variables are passed as a copy. So in the iDontTakeAPointer function it is receiving a copy of the tester struct then modifying the name field and then returning, which does nothing for us as it is modifying the copy and not the original.

like image 123
Datsik Avatar answered Sep 26 '22 02:09

Datsik