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 : List all packages and search for certain package
+29.6k error: trying to remove "yum", which is protected
+7.6k Golang : Gaussian blur on image and camera video feed examples
+8.1k Golang : Create and shuffle deck of cards example
+16.6k Golang : Get all upper case or lower case characters from string example
+61.4k Golang : Convert HTTP Response body to string
+11k Golang : 2 dimensional array example
+26.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+14k Golang : How to login and logout with JWT example
+5.7k Get Facebook friends working in same company
+25.2k Golang : Find files by extension
+10.3k Golang : Generate DSA private, public key and PEM files example