Introduction to Struct in Go Programming

Structs in Go Programming

Introduction

In this tutorial we’ll understand Struct in Golang, a Struct is another aggregate data type, another composite data type, as an aggregate type meaning groups together objects of arbitrary data types into one object.

The struct is short for structure, by the way, this takes also straight from C. We want to make a structure that describes a Student, a Student Struct. So, every Student has certain features a name, an address, and a phone number. if every Student, We want to represent these three different aspects of a Student.

Example

So, let’s give an example,

Student Struct
Name, Address, Phone

The struct is short for structure, by the way, this takes also straight from C. We want to make a structure that describes a Student, a Student Struct. So, every Student has certain features a name, an address, and a phone number. if every Student, We want to represent these three different aspects of a Student.

We have one option would be to have three separate variables. for every Student, we have three separate variables and the programmer has to remember that they are related. In that example, we have three pieces of information name, address, phone, they are related.

Now, another way to make a single Struct, that represents a Student and that Struct, it aggregates all three variables. So, this one object now contains a name, address, and phone number of one Student.

How does it Work?

We’re defining a type, a Struct type and we’re going to call it Student and this Student has three fields, three pieces of information, a name, address, and phone number.

type Struct Student
{
  name string
  addr string
  phone string
}

So, they are the same because they are related in the same object and so when you’re accessing them, it is obvious, that if you’re accessing them from the same object, you know that they are related to the same Student.

We can define any number of Students. we can declare var S1 is a Student, var S2 is a Student, and so on, and S1 in this case, it’s going to have a name, address, and phone number on its own.

type Struct Student
{
  name string
  addr string
  phone string
}
var S1 Student

So, each property is called a field, so the name is a field, the address is a field, the phone is a field. and S1 can have values for all those fields that are unique from another variable p2, which is a Student or p3. so we can have any number of Students in there.

Accessing Struct Fields

If we want to access the fields of the structure, by access, read from them or write to them, change them, we use dot notation.

 S1.name = "john"
 x = S1.addr

This is not like arrays. With arrays, you use square brackets, you use an index, use dot notation here. So, S1.name, if We want to assign that to John, We can say S1.name equals Joe and that will sign the field, the name field of S1 to string John.

Initializing structures

you can initialize them using the new() function. One way to make an empty Struct.

S1 := new(Student)

It initializes all the fields to zero, zero values. So, like in this Student structure, the values are all strings. the zero value is going to be the empty string.

We can initialize the structure is we can initialize it with a Struct literal. so if we want to give values to all the fields when you create the Student and we’re showing that here,

S1 := Student(name: "john", addr: "ac st", phone: "567")

In this case, we’re using a Struct literal and we’re making a new structure. but we’re also assigning all the fields to different values.

Leave a Reply

Your email address will not be published. Required fields are marked *