Structures in Golang

19 Nov, 2019
A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-oriented programming. It can be termed as a lightweight class that does not support inheritance but supports composition.
For Example, an address has a name, street, city, state, Pincode. It makes sense to group these three properties into a single structure address as shown below.
Declaring a structure:
 type Address struct {
      name string 
      street string
      city string
      state string
      Pincode int
}

In the above, the type keyword introduces a new type. 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.
Note: We can also make them compact by combining the various fields of the same type as shown in the below example:
type Address struct {
    name, street, city, state string
    Pincode int
}

To Define a structure: The syntax for declaring a structure:
var a Address

The above code creates a variable of a type Address which is by default set to zero. For a struct, zero means all the fields are set to their corresponding zero value. So the fields name, street, city, state are set to “”, and Pincode is set to 0.
You can also initialize a variable of a struct type using a struct literal as shown below:
var a = Address{"Akshay", "PremNagar", "Dehradun", "Uttarakhand", 252636}
Note:
Always pass the field values in the same order in which they are declared in the struct. Also, you can’t initialize only a subset of fields with the above syntax.
Go also supports the name: value syntax for initializing a struct (the order of fields is irrelevant when using this syntax). And this allows you to initialize only a subset of fields. All the uninitialized fields are set to their corresponding zero value.Example:var a = Address{Name:”Akshay”, street:”PremNagar”, state:”Uttarakhand”, Pincode:252636} //city:””
Example:
var a = Address{Name:”Akshay”, street:”PremNagar”, state:”Uttarakhand”, Pincode:252636} //city:””
// Golang program to show how to// declare and define the struct  package main  import "fmt"  // Defining a struct typetype Address struct {    Name    string    city    string    Pincode int}  func main() {      // Declaring a variable of a `struct` type    // All the struct fields are initialized     // with their zero value    var a Address     fmt.Println(a)      // Declaring and initializing a    // struct using a struct literal    a1 := Address{"Akshay", "Dehradun", 3623572}      fmt.Println("Address1: ", a1)      // Naming fields while     // initializing a struct    a2 := Address{Name: "Anikaa", city: "Ballia",                                 Pincode: 277001}      fmt.Println("Address2: ", a2)      // Uninitialized fields are set to    // their corresponding zero-value    a3 := Address{Name: "Delhi"}    fmt.Println("Address3: ", a3)}
Output:
{  0}
Address1:  {Akshay Dehradun 3623572}
Address2:  {Anikaa Ballia 277001}
Address3:  {Delhi  0}

To access individual fields of a struct you have to use dot (.) operator.
Example:
// Golang program to show how to// access the fields of structpackage main  import "fmt"  // defining the structtype Car struct {    Name, Model, Color string    WeightInKg         float64}  // Main Functionfunc main() {    c := Car{Name: "Ferrari", Model: "GTC4",            Color: "Red", WeightInKg: 1920}      // Accessing struct fields    // using the dot operator    fmt.Println("Car Name: ", c.Name)    fmt.Println("Car Color: ", c.Color)      // Assigning a new value    // to a struct field    c.Color = "Black"          // Displaying the result    fmt.Println("Car: ", c)}
Output:
Car Name:  Ferrari
Car Color:  Red
Car:  {Ferrari GTC4 Black 1920}

Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. You can also create a pointer to a struct as shown in the below example:
// Golang program to illustrate// the pointer to structpackage main  import "fmt"  // defining a structuretype Employee struct {    firstName, lastName string    age, salary int}  func main() {      // passing the address of struct variable    // emp8 is a pointer to the Employee struct    emp8 := &Employee{"Sam", "Anderson", 55, 6000}      // (*emp8).firstName is the syntax to access    // the firstName field of the emp8 struct    fmt.Println("First Name:", (*emp8).firstName)    fmt.Println("Age:", (*emp8).age)}
Output:
First Name: Sam
Age: 55

The Golang gives us the option to use emp8.firstName instead of the explicit dereference (*emp8).firstName to access the firstName field. Example to show this is following:
// Golang program to illustrate// the pointer to structpackage main  import "fmt"  // Defining a structuretype Employee struct {    firstName, lastName string    age, salary         int}  // Main Functionfunc main() {      // taking pointer to struct    emp8 := &Employee{"Sam", "Anderson", 55, 6000}      // emp8.firstName is used to access    // the field firstName    fmt.Println("First Name: ", emp8.firstName)    fmt.Println("Age: ", emp8.age)}
Output:
First Name:  Sam
Age:  55

Golang
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to concatenate two strings in Golang
time.Sleep() Function in Golang With Examples
strings.Contains Function in Golang with Examples
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
Golang Maps
Time Formatting in Golang
Interfaces in Golang
Different Ways to Find the Type of Variable in Golang
How to Parse JSON in Golang?