How to Copy Struct Type Using Value and Pointer Reference in Golang? Last Updated : 22 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report A structure or struct in Golang is a user-defined data type that allows to combine data types of different kinds and act as a record. A struct variable in Golang can be copied to another variable easily using the assignment statement(=). Any changes made to the second struct will not be reflected back to the first struct. Example 1: C // Golang program to illustrate copying // a structure to another variable package main import ( "fmt" ) // declaring a structure type Student struct{ // declaring variables name string marks int64 stdid int64 } // main function func main() { // creating the instance of the // Student struct type std1 := Student{"Vani", 98, 20024} // prints the student struct fmt.Println(std1) // copying the struct student // to another variable by // using the assignment operator std2 := std1 // printing copied struct // this will have same values // as struct std1 fmt.Println(std2) // changing values of struct // std2 after copying std2.name = "Abc" std2.stdid = 20025 // printing updated struct fmt.Println(std2) } Output: {Vani 98 20024} {Vani 98 20024} {Abc 98 20025} In the case of pointer reference to the struct, the underlying memory location of the original struct and the pointer to the struct will be the same. Any changes made to the second struct will be reflected in the first struct also. Pointer to a struct is achieved by using the ampersand operator(&). It is allocated on the heap and its address is shared. Example 2: C // Golang program to illustrate the // concept of a pointer to a struct package main import ( "fmt" ) // declaring a structure type Person struct{ // declaring variables name string address string id int64 } // main function func main() { // creating the instance of the // Person struct type p1 := Person{"Vani", "Delhi", 20024} // prints the student struct fmt.Println(p1) // referencing the struct person // to another variable by // using the ampersand operator // Here, it is the pointer to the struct p2 := &p1 // printing pointer to the struct fmt.Println(p2) // changing values of struct p2 p2.name = "Abc" p2.address = "Hyderabad" // printing updated struct fmt.Println(p2) // struct p1 values will // also change since values // of p2 were also changed fmt.Println(p1) } Output: {Vani Delhi 20024} &{Vani Delhi 20024} &{Abc Hyderabad 20024} {Abc Hyderabad 20024} Comment More infoAdvertise with us Next Article How to Copy Struct Type Using Value and Pointer Reference in Golang? V vanigupta20024 Follow Improve Article Tags : Go Language Golang-Pointers Golang-Program Similar Reads 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: Example: Here, you ca 1 min read How to find the type of Struct in Golang? A structure or struct in Golang is a user-defined data type which is a composition of various data fields. Each data field has its own data type, which can be a built-in or another user-defined type. Struct represents any real-world entity that has some set of properties/fields. Go does not support 3 min read How to create a Struct Instance Using a Struct Literal in Golang? A structure or struct in Golang is a user-defined data type which is a composition of various data fields. Each data field has its own data type, which can be a built-in or another user-defined type. Struct represents any real-world entity which has some set of properties/fields. For Example, a stud 4 min read How to print struct variables data in Golang? A struct (Structure) is a user-defined type in Golang that contains a collection of named fields/properties which creates own data types by combining one or more types. Also, this concept is generally compared with the classes in object-oriented programming. A struct has different fields of the same 2 min read How to copy one slice into another slice in Golang? In Go, a Slice is a variable-length sequence that can hold elements of the same type. You canât mix different types of elements in a single slice. To copy one slice into another, Go provides a built-in function called copy(). This function allows you to duplicate the elements from one slice (the sou 3 min read Like