English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Golang, a struct is a user-defined type that allows grouping of items of possibly different types/Composed into a single type. Any real-world entity that has a set of properties/The entities of the fields can all be represented as structures. This concept is often compared to classes in object-oriented programming. It can be called a lightweight class that does not support inheritance but supports composition.
For example, an address has name, street, city, state, Pincode. As shown below, combine these three properties into a structureAddressIt makes sense.
Declare the structure:
type Address struct { name string street string city string state string Pincode int }
On the above,typeA keyword introduces a new type. It is followed by the name of the type (Address)and keywordstruct,To illustrate the structure we are defining. This structure contains a list of fields within the curly braces. Each field has a name and a type.
Note:We can also make them compact by combining fields of the same type, as shown in the following example:}}
type Address struct { name, street, city, state string Pincode int }
Define structure:Syntax for declaring a structure:
var a Address
The above code creates aAddress typeVariables, by default, they are set to zero. For structures, zero means that all fields are set to their corresponding zero values. Therefore, the fields name, street, city, state are set to "", and Pincode is set to 0.
You can alsoInitialize a variable of a structure type using a structure literalAs shown below:
var a = Address{"Akshay", "PremNagar", "Dehradun", "Uttarakhand", 252636}
Note:
Always pass field values in the order they are declared in the structure. Similarly, you cannot initialize a subset of fields using the above syntax.
Go also supportsName: valueSyntax for initializing a structure (the order of fields is irrelevant when using this syntax). Moreover, this syntax only allows you to initialize a subset of fields. All uninitialized fields are set to their respective zero values.
For example:
var a = Address{"Akshay", "PremNagar", "Dehradun", "Uttarakhand", 252636}
package main import \ //Define structure type type Address struct { Name string city string Pincode int } func main() { //Declare a variable of type "struct" //Initialize all struct fields //Its value is zero var a Address fmt.Println(a) //struct // Use structure literals to declare and initialize a1 := Address{"Akshay", "Dehradun", 3623572} fmt.Println("Address"1: "a"1) //When naming fields //Initialize a structure a2 := Address{Name: "Anikaa", city: "Ballia", Pincode: 277001} fmt.Println("Address"2: "a"2) //Uninitialized fields are set to //They correspond to zero values a3 := Address{Name: "Delhi"} fmt.Println("Address"3: "a"3) }
Output:
{ 0} Address1: {Akshay Dehradun} 3623572} Address2: {Anikaa Ballia} 277001} Address3: {Delhi 0}
To accessOf the structureEach field, you must use a dot(.)Operator.
package main import \ //Define a structure type Car struct { Name, Model, Color string WeightInKg float64 } func main() { 4", Color: \ 1920} //Access a struct field //Using the dot operator fmt.Println("Car Name: ", c.Name) fmt.Println("Car Color: ", c.Color) //Assign a new value //Point to a struct field c.Color = "Black" //Display the result fmt.Println("Car: ", c) }
Output:
Car Name: {Ferrari Car Color: {Red Car: {Ferrari GTC4 Black 1920}
In Golang, a pointer is a variable used to store the memory address of another variable. You can also create a pointer to a struct, as shown in the following example:
// Pointer to a struct package main import \ // Define a structure type Employee struct { firstName, lastName string age, salary int } func main() { //Passing the address of a struct variable // emp8 emp8 := &Employee{\"Sam\", \"Anderson\", 55, 6000} //* emp8, // emp8The firstName field of the structure fmt.Println("First Name:", (*emp8).firstName) fmt.Println("Age:", (*emp8).age) }
Output:
First Name: Sam Age: 55
Golang provides us with the option to use emp8.firstName instead of explicitly dereferencing(* emp8).firstName to access the firstName field. The following example shows:
//Pointer to a structure package main import \ //Define a structure type Employee struct { firstName, lastName string age, salary int } func main() { //Obtaining a pointer to a struct emp8 := &Employee{\"Sam\", \"Anderson\", 55, 6000} // emp8is used to access //Field firstName fmt.Println("First Name: ", emp8.firstName) fmt.Println("Age: ", emp8.age) }
Output:
First Name: Age: 55