I am almost done with an issue that has stomped me as I am new to Golang, I am basically trying to get the absolute path of a file inside the os.open method. I have been trying all types of things but nothing works
func UploadProfile(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
infile, header, err := r.FormFile("upload_file")
if err != nil {
http.Error(w, "Error parsing uploaded file: "+err.Error(), http.StatusBadRequest)
return
}
defer infile.Close()
absolue_path := string(filepath.Abs(header.Filename))
// I want to get the absolute path in os.Open
file, err := os.Open(absolute_path)
}
for instance if I hard code the string in the os.Open like /Users/Documents/pictures/cats.jpg then the file uploads successfully . When i try to get the absolute path and put it inside the os.Open I get this error on runtime multiple-value filepath.Abs() in single-value context . Is there any other way that I can get the path of the file so that I can put it inside that method ?
According to documentation, Abs
function will return TWO values, one string and one error.
So you can not have something like:
absolute_path := string(filepath.Abs(header.Filename))
instead, you should write:
absolute_path, err := filepath.Abs(header.Filename)
Also note that absolute_path is a string already.
The compiler error from this line of code (multiple-value filepath.Abs() in single-value context)
absolue_path := string(filepath.Abs(header.Filename))
is telling you that filepath.Abs
is returning multiple arguments and string
only takes one argument.
filepath.Abs
returns a string
and error
Code should be:
ap, err := filepath.Abs(header.Filename)
if err != nil {
// handle error
}