Structs in Golang

Structs in Golang

What is struct In Go? 

A struct is the short form of the terminology “structure” in Go. A struct is a user-defined type that represents the collection of fields. Logically, it’s used to collect data about a particular field of research.  

Go plays a very important role in the functioning of the struct. It’s an in-build struct type in Golang.

Defining structs

The structs are defined with the keyword of “type”. Further, it is followed by the name of the type called workers. 

In addition to this, The term Struck identifies that we are creating a struck. Afterward, within the curly braces, a collection of fields is mentioned with its name & type. 

type Workers struct {

    Name       string

    Age        int

    Experience float64

    Weight     int

}

Why use structs? 

Well, You have understood the concept that structs are meant to keep things together into variables. 

In this section, you’ll be able to understand the need and certain reasons to make use of structs in the Go program language

The various reason to use them is as follows – 

  • The main reason behind using a struck when you have a data type that has more than one field. Because struct is simply a collection of fields. 
  • You’ll have a clear, fair, and proper record of the data that you have kept within structs in Go.
  • You can also hire golang developer in order to get your codes run and format the structures. 
  • With the help of structs in Go, You can make blocks in your data. Which eventually smooths your data relationship. 
  • A paramount benefit of using struck is that it helps to keep all the data within one single variable. 

Different Declaration of Structs In Go

Now, Let’s see the declaration of structs in the golang program language.

so, let’s dive into this code to see how struct works. 

package main

import (

    “fmt”

)

type Workers struct {

    Name       string

    Age        int

    Experience float64

    Weight     int

}

func main() {

    fmt.Println(“Struct in Golang”)

    Pradeep:= Workers{“Pradeep”, 38, 4.5, 70}

    fmt.Println(Pradeep)

}

In the above code, The user type struct is workers in which we have listed their name, age, experience, weight, etc. 

Hence, we put these ‘product-type structs’ into the ‘user-type structs’. As you can also see in the above code.

Now, Let’s see how it’ll look in VS code terminal.

This is the whole procedure of declaration of structs in Go.

Well, sometimes, the given struct type seemed not to be sufficient for developers. So, They willingly differ or make variations in the existing code. 

Alternative Variation Declaration in Structs  

Under this variation, We have made a global variable named Pardeep. 

The developer has willingly made a local variable, where they can keep the details of a worker altogether.  

package main

import (

    “fmt”

)

type Workers struct {

    Name       string

    Age        int

    Experience float64

    Weight     int

}

var Workers1 Workers

func main() {

    fmt.Println(“Struct in Golang”)

    Pradeep:= Workers{“Pradeep”, 38, 4.5, 70}

    fmt.Println(Pradeep)

    Workers1.Name = “sana”

    Workers1.Age = 25

    Workers1.Experience = 5.2

    Workers1.Weight = 68

    fmt.Println(Workers1)

}

Now, Let’s take a look at how this code runs in Go. 

Conclusion 

At the end of this article hopefully, you have made a clear understanding of the overall concept i.e. what are structs, why & how to use structs with its Different Declaration of structs in Go.

Furthermore, here are some FAQs mentioned to get your basic understanding clear. You can also make your concepts clear after running such codes by yourself. 

Thanks for reading the blog!!! 

Frequently Asked Question

What are structs in Golang?

Structs are short for structure. They are the user-defined type that keeps the collection of fields. It basically contains various fields in one single variable.

Why do we use struct in Go?

The Reason behind using struct is that it can be used to restore data at all professional and larger scales. So, Struct is personally recommended by many professionals.