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
+3.9k MariaDB/MySQL : Form select statement or search query with Chinese characters
+10.5k Golang : Sort and reverse sort a slice of runes
+3.9k Unix/Linux : How to pipe/save output of a command to file?
+28.9k Golang : Generate random string
+21.7k Golang : Randomly pick an item from a slice/array example
+8.2k Golang : does not implement flag.Value (missing Set method)
+5k Golang : Shuffle array of list
+5.5k Golang : Find the longest line of text example
+8.9k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+15.1k Golang : Check if a string contains multiple sub-strings in []string?