My function is working file with this signature
func GetFileMetadata(f *multipart.FileHeader) *FileMetadata {
d := new(FileMetadata)
// ...
if typ, err := GetFileRealType(f); err == nil {
d.Type = typ.MIME.Value
fileExt = "." + typ.Extension
} else {
d.Type = "unknown"
}
return d
}
With following signature it doesn’t.
func GetFileMetadata(f *multipart.FileHeader) (d *FileMetadata) {
// ...
if typ, err := GetFileRealType(f); err == nil {
d.Type = typ.MIME.Value
fileExt = "." + typ.Extension
} else {
d.Type = "unknown"
}
return
}
Getting nil pointer dereference err. What is the reason?
>Solution :
When you use a named return variable, that variable is declared within the function body with an initial value set to its zero value. So in your second example where you used a named return with d, d is a nil pointer. You should still initialize it:
d = new(FileMetadata)