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
+6k Golang : Process non-XML/JSON formatted ASCII text file example
+17.2k Golang : Clone with pointer and modify value
+3.5k Java : Random alphabets, alpha-numeric or numbers only string generator
+10.2k Generate Random number with math/rand in Go
+7.2k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+12.4k Golang : Pass database connection to function called from another package and HTTP Handler
+9.7k Golang : Setting variable value with ldflags
+20.5k Golang : Convert PNG transparent background image to JPG or JPEG image
+17.5k Golang : Qt image viewer example
+13.2k Golang : Read from buffered reader until specific number of bytes
+22.8k Golang : Randomly pick an item from a slice/array example
+5.4k Unix/Linux/MacOSx : Get local IP address