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 can see we are instantiating a Struct using new keyword.
// Golang program to show how to instantiate // Struct using the new keyword package main import "fmt" type emp struct { name string empid int salary int} func main() { // emp1 is a pointer to // an instance of emp // using new keyword emp1 := new(emp) emp1.name = "XYZ" emp1.empid = 1555 emp1.salary = 25000 fmt.Println(emp1) // emp2 is an instance of emp var emp2 = new(emp) emp2.name = "ABC" emp2.salary = 35000 fmt.Println(emp2) } |
chevron_right
filter_none
Output:
&{XYZ 1555 25000}
&{ABC 0 35000}
Recommended Posts:
- How to instantiate Struct Pointer Address Operator in Golang?
- How to create a Struct Instance Using a Struct Literal in Golang?
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- How to add a method to struct type in Golang?
- How to find the type of Struct in Golang?
- How to Compare Equality of Struct, Slice and Map in Golang?
- How to Assign Default Value for Struct Field in Golang?
- How to print struct variables data in Golang?
- Golang Program to Use Field Tags in the Definition of Struct Type
- JavaScript | new Keyword
- New self vs. new static in PHP
- How to Create Custom Errors using New Function in Golang?
- Golang program that uses fallthrough keyword
- Range Keyword in Golang
- Golang program that uses defer keyword
- Is sizeof for a struct equal to the sum of sizeof of each member?
- How to create an unordered_set of user defined class or struct in C++?
- How to implement is functionality without using is keyword in C#
- Invoking an overloaded constructor using this keyword in C#
- Add a new column in Pandas Data Frame Using a Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

