S
S
spongebozz2019-07-12 18:59:20
go
spongebozz, 2019-07-12 18:59:20

How to understand what type and structure are in Go?

Hello. I just can’t figure out what type and structure are in Go, how can I understand it easier? When is the best time to use them?
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Pavlyuk, 2019-07-12
@spongebozz

Think of it as just a way to group multiple variables into one. Structures should be used where it is more convenient to use a group of variables at once.
For example, we need to pass user data to the function. We can write

func RegisterUser(name string, surname string, age int)

But if we need to pass the same user data to another function, we will have to repeat all these three variables. And if we go to add another place of residence, for example, then we will have to look for all the places where we pass these three variables and add the fourth one. It is not comfortable. Therefore, we write:
type User struct {
    Name string
    Surname string
    Age int
}

func RegisterUser(user User)

Now, to change the data set, we just need to fix the type. Passing it as a single variable is more convenient. Yes, and logically the data is grouped more clearly.

P
Papa, 2019-07-12
Stifflera @PapaStifflera

A struct is a user-defined type.

R
Roman Mirilaczvili, 2019-07-12
@2ord

Structure is a set of characteristics of some object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question