Structure is a collection of variables of different types under a single name.
For example: You want to store some information about a person:
his/her name,
citizenship number and
salary.
You can easily create different variables name, citNo, salary to store these information separately.
In the future, you would want to store information about multiple persons. Now, you'd need to create different variables for each information per person: name1, cityNo1, salary1, name2, citNo2, salary2
A better approach will be to have a collection of all related information under a single name Person, and use it for every person. Now, the code looks much cleaner, readable and efficient as well.
This collection of all related information under a single name Person is a structure.
A sample example golang structure
Example structure program in C
Person1 and person2 are individual structure objects created for the main structure person.
Another way of creating a structure variable is:
Defining a Structure in go language programming :
To define a structure, you must use type and struct statements.
The struct statement defines a new data type, with more than one member for your program.
type statement binds a name with the type which is struct in our case.
Format of the structure :
After defining the structure type, it can be used to declare variables of that type using the below syntax.
A sample example golang structure
Accessing Structure Members
To access any member of a structure, we use the member access operator (.).
Member access operator is coded as a period between the structure variable name and the structure member that we wish to access. (person1.age , person1.name)
Use struct keyword to define variables of structure type.
Example to explain usage of structure:
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Output for the above go program
Structures as Function Arguments
Structure can be passed as a function argument in very similar way as you pass variable or pointer.
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Output for the above go program
Pointers to Structures
pointers to structures declaration is very similar way as you define pointer to any other variable as below,
Store the address of a structure variable in the above defined pointer variable.
To get the address of a structure variable, place the & operator before the structure's name as below,
To access the members of a structure using a pointer to that structure, you must use the "." operator as follows:
Program using structure pointer
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Related Searches to Structures In Golang | Golang Structures
golang new structgolang constructorgolang for loop arrayeffective goalgorithms and data structures in gogolang interfacesgolang for rangegolang typegolanggoprogoogle gogolang tutorialgoogle languagego languagego programming languagego languagego programming languagegolang tutorialgo googlegoogle gogolanggp languagego programming language bookgolang coursethe go programming languagebest book to learn golanggolang web development tutorialgolang big datago language bookbig data golanggolang booklearn golanggolang programsgo programming language usesgo to google go to googlelearn go programminggoogle and gowhat is go programming languagego google go google go googlewhich programming language used in googlego to the bookgo scripting languagegolang online tutorialgoogle language programminggo language programminggolang iotgo to go to googlewhy go languagego programming language tutorialgo language tutoriallearn go languagegolang jobsgoogle go softwareprogramming language gogo programing languagegolang web frameworkgo for googlego language ide for windowsgoogle golanggolang programminggoogle coding languagegolang projectsgo programming tutorialgoogle go language tutorialgolang debuggergo to the googlegoogle go programming languagegolang classgolang serverprogramming language used by googlegoogle go languagego programminggo programming pdfgolang developmentgolang frameworkgo language examplesgolang tutorial pdflanguage googlego programgo programming language pdf