N
N
NOONE2018-11-14 22:14:33
go
NOONE, 2018-11-14 22:14:33

I can not understand the meaning of the function in go?

Hi there is an example function

func (p *Page) save() error {
    filename := p.Title + ".txt"
    return ioutil.WriteFile(filename, p.Body, 0600)
}

I can’t understand, it’s kind of like not an anonymous function, its name is save, what then does it mean that it’s not the function parameters? because the parameters come after the name, or I don’t understand something func (p *page)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dasha Tsiklauri, 2018-11-14
@Djam36

This is a function of an object that is of type Page . In simple terms, you cannot call save() globally , but you must call the p.save() function on the object . Accordingly, access to the object will be organized inside the function.

type Page struct{}
func (p*Page) save() error{
  log.Println(p) // тут доступ к экземпляру у которого вызывается метод
  return nil
}
p:=&Page{}
err:=p.save()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question