Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Data in Structs | Intro to Structs & Maps
Introduction to GoLang

Data in StructsData in Structs

Now that we know how to create an instance of a structure, we should learn how to store and modify data in them.

We can use the dot (.) symbol followed by the field name to reference it. Consider the example in the following program, where we create an instance of the Student class called student1:

go

index.go

This is because no data was manually stored in the structure, causing each of the fields to take a default zero value based on its type.

Note

In Go, we cannot specify our own default values for the fields; it automatically assigns zero values to the fields based on their types. However, we can specify custom default values by creating a constructor function, which is beyond the scope of this course as it requires knowledge of pointers.

We can also reference and assign values to the fields using the same referencing method, for example:

go

index.go

Therefore, we can modify the program above to store some initial data accordingly:

go

index.go

Note

The fields of a struct are also referred to as members.

Which symbol do we use for accessing the struct members / fields:

Виберіть правильну відповідь

Все було зрозуміло?

Секція 6. Розділ 3
course content

Зміст курсу

Introduction to GoLang

Data in StructsData in Structs

Now that we know how to create an instance of a structure, we should learn how to store and modify data in them.

We can use the dot (.) symbol followed by the field name to reference it. Consider the example in the following program, where we create an instance of the Student class called student1:

go

index.go

This is because no data was manually stored in the structure, causing each of the fields to take a default zero value based on its type.

Note

In Go, we cannot specify our own default values for the fields; it automatically assigns zero values to the fields based on their types. However, we can specify custom default values by creating a constructor function, which is beyond the scope of this course as it requires knowledge of pointers.

We can also reference and assign values to the fields using the same referencing method, for example:

go

index.go

Therefore, we can modify the program above to store some initial data accordingly:

go

index.go

Note

The fields of a struct are also referred to as members.

Which symbol do we use for accessing the struct members / fields:

Виберіть правильну відповідь

Все було зрозуміло?

Секція 6. Розділ 3
some-alt