T
T
Timokins2020-04-01 12:30:58
go
Timokins, 2020-04-01 12:30:58

How to access structure from structure function?

type T struct {
  Count int
  Func func(string) err
}


Do I understand correctly that inside the function field of the structure, I will not get access to the structure without an explicit indication in the arguments?

type T struct {
  Count int
  Func func(*T, string) err
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2020-04-01
@timokins

Yes, there is no way you can access the struct unless you explicitly pass it to the function.
You are doing something weird, maybe you wanted to do this:

type T struct {
  Count int
}

func (t *T) Func(s string) error {
    t.Count // <- получение доступа к полю структуры
}

P
Pavel Shvedov, 2020-04-01
@mmmaaak

in go, the methods of structures are declared like this , and there access to the structure will be

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question