Open In App

Anonymous Structure and Field in Golang

Last Updated : 12 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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

    Similar Reads

    Recursive Anonymous Function in Golang
    Recursion is a process in which a function calls itself implicitly or explicitly and the corresponding function is called recursive function. Go language supports special feature called anonymous function. It is a function that doesn’t contain any name. It is used to create an inline function. Recursive functions can be declared & defined as an
    2 min read
    Function as a Field in Golang Structure
    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. As we know that in Go language function is also a user-defined type so, you are allowed to create a function fie
    2 min read
    reflect.Field() Function in Golang with Examples
    Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Field() Function in Golang is used to get the i'th field of the struct v. To access this function, one needs to imports the reflect package in the program. Syntax:
    2 min read
    How to use Field Tags in the Definition of Struct Type in Golang?
    Golang provide structures for the definition of a custom datatype. The concept of structure in Go is similar to the structure in C/C++. Example: type Person struct { Name string Aadhaar int Street string HouseNo int } Structures in Golang can be written to files like JSON for storing data on a hard drive or for sending over the network. JSON is a l
    2 min read
    How to Assign Default Value for Struct Field in Golang?
    Default values can be assigned to a struct by using a constructor function. Rather than creating a structure directly, we can use a constructor to assign custom default values to all or some of its members. Example 1: // Golang program to assign // default values to a struct // using constructor function package main import ( "fmt" ) // d
    2 min read
    Golang Program to Use Field Tags in the Definition of Struct Type
    Structure in Golang is used to store different types of data in one place. 'struct' itself is a user-defined data type. The syntax of 'struct' in Golang is as follow: Syntax: type var_name struct { var1 data_type var2 data_type } Structure in Golang is written to files like JSON. It is a data storage format. Golang provides packages in the standard
    2 min read
    Anonymous function in Go Language
    Go language provides a special feature known as an anonymous function. An anonymous function is a function which doesn't contain any name. It is useful when you want to create an inline function. In Go language, an anonymous function can form a closure. An anonymous function is also known as function literal. Syntax: func(parameter_list)(return_typ
    2 min read
    Structure Equality 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. This concept is generally compared with the classes in object-oriented programming. It can be termed as a lightw
    3 min read
    Nested Structure 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. Go language allows nested structure. A structure which is the field of another structure is known as Nested Stru
    3 min read
    Promoted Fields in Golang Structure
    In Go structure, promoted fields are just like anonymous fields, the type of the field is the name of the field. We use this concept in the nested structure where a structure is a field in another structure, simply by just adding the name of the structure into another structure and it behaves like the Anonymous Field to the nested structure. And th
    2 min read
    Article Tags :