go – Type has no field or method-ThrowExceptions

Exception or error:

I have a problem with a struct’s method.
In one file (same package) I a struct:

type Task struct {
    ID       int    `json:"id"`
    Priority string `json:"priority"`
    Content  string `json:"content"`

The Task comes as a body in POST. Parsing the JSON to this struct works well.
In other file I have a method:

func (task *Task) createTask() (err error) {
    data, err := bson.Marshal(&task)
    if err != nil {
        return errors.New("error in parsing incoming task")
    rslt, err := collection.InsertOne(context.Background(), data)
    if err != nil {
        return errors.New("error in saving task to database")
    return nil

The Task structure is in the same file with my server. createTask() method is in the file which handles communication to mongoDB.
When I run my server I have this error:

./server.go:53:12: task.createTask undefined (type Task has no field or method createTask)

Here is how I am calling the method:

var task Task
json.Unmarshal(body, &task)
err = task.createTask()

Do you have any ideas what am I doing wrong?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *