-2

my errors.go file

 package errors

import (
    "net/http"
)

type RestError struct {
    Message string `json:"message"`
    Status  int    `json:"status"`
    Error   string `json:"error"`
}

func (err *RestError) NewBadRequestError(message string) {
    err.Message = message
    err.Status = http.StatusBadRequest
    err.Error = "bad_request"
}

my user.go file

 package users

import (
    errors "Utils/Errors"
)

type (
    User struct {
        ID       uint   `json:"id"`
        Name     string `json:"name"`
        LastName string `json:"last_name"`
        Email    string `json:"email"`
    }
)

func (u *User) Validate() (err *errors.RestError) {

    if u.Name == "" {
        return &err.NewBadRequestError("The name field can't be empty")
    }

    return nil
}

i always get the following compiler error

err.NewBadRequestError(*message) (no value) used as valuecompiler

Would like to know what i'm doing wrong. I also tried return &err.NewBadRequestError(message: "The name field can't be empty") but still get the following syntax error : missing ',' in argument listsyntax

Gaetan Sobze
  • 225
  • 2
  • 5
  • 13
  • This isn't causing your immediate problem, but `import ( errors "Utils/Errors" )` is invalid. [Go does not support relative imports](https://stackoverflow.com/questions/38517593/relative-imports-in-go). – Jonathan Hall Mar 28 '20 at 16:29
  • Looks like you have a typo? you meant to do &erros.NewBadRequestError("The name field can't be empty") ? --- err => errors @Gaetan Sobze – gipsy Mar 28 '20 at 17:01

1 Answers1

2

Your problem has nothing to do with separate packages.

First, err isn't defined. I'm assuming you have it defined somewhere not shown in the code you pasted. If this is accurate...

return &err.NewBadRequestError("The name field can't be empty")

Doesn't make any sense.

First, err.NewBadRequestError() doesn't return anything, so there is nothing to return.

Second, you've have a &, which means to take the address of the value, but once again, there's no value--this is what the error is telling you.

It's not clear what your code is meant to do, so I can't tell you exactly how to fix it. One option is just to remove return & from your code:

    if u.Name == "" {
        err.NewBadRequestError("The name field can't be empty")
    }

Another would be to modify NewBadRequestError to return some value.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • The error.go file is a custom error handling file. When i have a bad request, i return a NewBadRest func that receive the RestErr struct. if u.Name == "" { err.NewBadRequestError("The name field can't be empty") } means if i have an empty name, i return a badrequest response custom error – Gaetan Sobze Mar 28 '20 at 16:51