Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang how can I get full file path

Tags:

go

I been searching around and can not find a way to get the full file path in Go . I have a regular html form and then I try to get all the information in the backend

<form method="post" enctype="multipart/form-data"  action="/uploads">
    <p><input type="file" name="my file" id="my file"></p>
    <p>
        <input type="submit" value="Submit">
    </p>

func upload() {

    f,h,err := r.FormFile("my file")
    if err != nil {
        log.Println(err)
        http.Error(w,"Error Uploading",http.StatusInternalServerError)
        return
    }
    defer  f.Close()
   println(h.Filename)
    }

// This gets me the name of the file, I would like the full path of it

I have tried file path.dir() but that does not do anything

like image 303
Pablo prez Avatar asked Dec 19 '22 11:12

Pablo prez


1 Answers

here is an example:

package main

import (
   "fmt"
   "path/filepath"
)

func main() {
   abs,err := filepath.Abs("./hello.go")
   if err == nil {
      fmt.Println("Absolute:", abs)
   }
}

like image 143
Bill Zelenko Avatar answered Jan 19 '23 02:01

Bill Zelenko