Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Creating Instances of Structs | Intro to Structs & Maps
Introduction to GoLang

bookCreating Instances of Structs

In the previous chapter, we learned how to define a structure. However, the definition serves as only a blueprint, specifying how data is stored in an instance of that structure.

An instance of a structure is essentially a variable with the structure as its data type. Since it's a variable, data can be stored and modified within it.

Consider the following example of the Person structure from the previous chapter:

index.go

index.go

copy
12345
type Person struct { name string age int salary float64 }

We can create an instance of Person using the following syntax:

index.go

index.go

copy
1
var person1 Person

Note

In code, the structure must be defined before its instances are created.

question mark

How would you create an instance called stud1 of the following class:

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 1.96

bookCreating Instances of Structs

Sveip for å vise menyen

In the previous chapter, we learned how to define a structure. However, the definition serves as only a blueprint, specifying how data is stored in an instance of that structure.

An instance of a structure is essentially a variable with the structure as its data type. Since it's a variable, data can be stored and modified within it.

Consider the following example of the Person structure from the previous chapter:

index.go

index.go

copy
12345
type Person struct { name string age int salary float64 }

We can create an instance of Person using the following syntax:

index.go

index.go

copy
1
var person1 Person

Note

In code, the structure must be defined before its instances are created.

question mark

How would you create an instance called stud1 of the following class:

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 2
some-alt