Thursday, December 26, 2024
Google search engine
HomeLanguagesGolangAnonymous Structure and Field in Golang

Anonymous Structure and Field in Golang

A structure or struct in Golang is a user-defined type, which allows us to create a group of elements of different types into a single unit. Any real-world entity which has some set of properties or fields can be represented as a struct.

Anonymous Structure

In Go language, you are allowed to create an anonymous structure. An anonymous structure is a structure which does not contain a name. It useful when you want to create a one-time usable structure. You can create an anonymous structure using the following syntax:

variable_name := struct{
// fields
}{// Field_values}

Let us discuss this concept with the help of an example:

Example:




// Go program to illustrate the
// concept of anonymous structure
package main
  
import "fmt"
  
// Main function
func main() {
  
    // Creating and initializing
    // the anonymous structure
    Element := struct {
        name      string
        branch    string
        language  string
        Particles int
    }{
        name:      "Pikachu",
        branch:    "ECE",
        language:  "C++",
        Particles: 498,
    }
  
    // Display the anonymous structure
    fmt.Println(Element)
}


Output:

{Pikachu ECE C++ 498}

Anonymous Fields

In a Go structure, you are allowed to create anonymous fields. Anonymous fields are those fields which do not contain any name you just simply mention the type of the fields and Go will automatically use the type as the name of the field. You can create anonymous fields of the structure using the following syntax:

type struct_name struct{
    int
    bool
    float64
}

Important Points:

  • In a structure, you are not allowed to create two or more fields of the same type as shown below:
    type student struct{
    int
    int
    }
    

    If you try to do so, then the compiler will give an error.

  • You are allowed to combine the anonymous fields with the named fields as shown below:
    type student struct{
     name int
     price int
     string
    }
    
  • Let us discuss the anonymous field concept with the help of an example:

    Example:




    // Go program to illustrate the
    // concept of anonymous structure
    package main
      
    import "fmt"
      
    // Creating a structure
    // with anonymous fields
    type student struct {
        int
        string
        float64
    }
      
    // Main function
    func main() {
      
        // Assigning values to the anonymous
        // fields of the student structure
        value := student{123, "Bud", 8900.23}
      
        // Display the values of the fields
        fmt.Println("Enrollment number : ", value.int)
        fmt.Println("Student name : ", value.string)
        fmt.Println("Package price : ", value.float64)
    }

    
    

    Output:

    Enrollment number :  123
    Student name :  Bud
    Package price :  8900.23
    

    Previous article
    Next article
    Dominic Rubhabha-Wardslaus
    Dominic Rubhabha-Wardslaushttp://wardslaus.com
    infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
    RELATED ARTICLES

    Most Popular

    Recent Comments