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
+9.2k Golang : Play .WAV file from command line
+8.7k Golang : GMail API create and send draft with simple upload attachment example
+10.1k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+52.1k Golang : How to get struct field and value by name
+7k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+6k Golang & Javascript : How to save cropped image to file on server
+8.5k Golang : On lambda, anonymous, inline functions and function literals
+7.1k Golang : Accessing dataframe-go element by row, column and name example
+11.8k Golang : Save webcamera frames to video file
+6.4k Golang : Embedded or data bundling example
+5.3k Golang : Stop goroutine without channel