Table of contents
1.
Introduction
2.
Structure in Golang
2.1.
Syntax
3.
Anonymous Structure in Golang
3.1.
Syntax
3.2.
Example
3.3.
Output
4.
Anonymous Fields in Golang
4.1.
Syntax
4.2.
Example
4.3.
Output
5.
Frequently Asked Questions
5.1.
What is a structure?
5.2.
What do you mean by the Anonymous structure?
5.3.
When should I use the Anonymous structure?
5.4.
What do you mean by the Anonymous field?
6.
Conclusion
Last Updated: Mar 27, 2024

Anonymous Structure and Fields in Go

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! We hope that you are doing great.

Go(also known as Golang or Go language) is a statically typedcompiled programming, an open-source programming language used for general purposes designed by Google in 2007. The main intention of creating Go was to design a programming language to address criticism of other languages by keeping their characteristics the same. 

Now Go is widely used at Google. Even many other organisations are opting for Go. It is also used in many open-source projects.

If you want to learn more about Go, then follow these articles, GoWhat is Golang?, Introduction to Go.
 

Today, we will be discussing the Anonymous Structure and Fields in Go.
 

This blog will help you to extend your knowledge about the Go. So, without further ado, let’s start our discussion.

Structure in Golang

structure in Golang is a user-defined type, which allows you to combine a group of elements, possibly of different types, into a single type.

Structures are used to represent different types of records and real-world entities. This concept is somehow similar to Classes in Object-Oriented Programming.

Syntax

If you want to learn more about structs, follow this article Structs in Go.

Anonymous Structure in Golang

An Anonymous structure in Golang is a structure which does not have any name in its definition. As the Anonymous structure does not have any name, it can’t be referenced elsewhere in the program. It is a one-time usable structure.

Syntax

The syntax of the Anonymous structure in Golang is given below:

Example

The example is shown below:

package main
 
import "fmt"
 
// main function
func main() {
 
    // creating the anonymous structure named Element
    Element := struct {
        name string
        age int
        Dept string
        Year int
    }{
        // initializing the anonymous structure
        name: "xyz",
        age: 20,
        Dept: "IT",
        Year: 2023,
    }
 
    // displaying the anonymous structure
    fmt.Println(Element)
}

Output

Anonymous Fields in Golang

Like the Anonymous structure in Golang, a field in a structure can’t have any name. These are known as the Anonymous fields.

In this case, the type will become the field name.

Syntax

The syntax of the Anonymous fields is given below:

Example

package main
import "fmt"

type student struct{
    name string
    id int
    dept string
    string
}

// main function
func main() {

    // Assigning values to the anonymous field and named fileds
    value := student{"xyz" , 111, "CST", "2022"}
    fmt.Println("Name: " , value.name)
    fmt.Println("Id: ", value.id)
    fmt.Println("Dept: ", value.dept)
    fmt.Println("Passout: ", value.string) // accessing the anonymous filed using the type name

}

Output

Frequently Asked Questions

What is a structure?

A structure is a user-defined type which allows you to combine a group of elements, possibly of different types, into a single type.
 

What do you mean by the Anonymous structure?

An Anonymous structure in Golang is a structure which does not have any name in its definition.
 

When should I use the Anonymous structure?

As the Anonymous structure does not contain any name, it can’t be referenced elsewhere in the program, thus for the cases when the structure is meant to use only once.
 

What do you mean by the Anonymous field?

If a field in a structure does not contain any name, it is known as the Anonymous field.

Conclusion

In this article, we have extensively discussed Anonymous Structure and Fields in Go.

We started with the basic introduction, then we discussed,

  • About the Structure in Golang
  • Anonymous Structure in Go
  • Anonymous Fields in Go
     

You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

Happy Reading!

Live masterclass