Get file path of temporary file in Go
There are time when we need to know the exact file path of a temporary file. This is a a short tutorial on how to get the file path of a temporary file in Go.
gettempfilepath.go
package main
import (
"fmt"
"os"
"io/ioutil"
"path/filepath"
)
func main () {
file, err := ioutil.TempFile(os.TempDir(), "temp")
if err != nil {
panic(err)
}
fmt.Println("Temp File created!")
thepath, err := filepath.Abs(filepath.Dir(file.Name()))
if err != nil {
panic(err)
}
fmt.Println("The file path : ", thepath)
defer os.Remove(file.Name())
}
See also : Golang : Get current file path of a file or executable
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4.9k Golang : The Tao of importing package
+16k CodeIgniter/PHP : Create directory if does not exist example
+7k Golang : Create zip/ePub file without compression(use Store algorithm)
+16.6k Golang : Find file size(disk usage) with filepath.Walk
+11.7k Golang : Simple client-server HMAC authentication without SSL example
+17.9k Golang : Get path name to current directory or folder
+12.3k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+30.4k Golang : Calculate percentage change of two values
+51.5k Golang : How to get time in milliseconds?
+8.4k Golang : Heap sort example
+15.2k Golang : How to login and logout with JWT example