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
+21.1k Golang : Sort and reverse sort a slice of strings
+6.4k CodeIgniter : form input set_value cause " to become & quot
+22.2k Golang : Securing password with salt
+8.3k Golang : Auto-generate reply email with text/template package
+17.9k Golang : How to make a file read only and set it to writable again?
+17.7k Golang : Read data from config file and assign to variables
+29.9k Golang : How to get HTTP request header information?
+5.3k Golang : Get FX sentiment from website example
+9.9k Golang : ffmpeg with os/exec.Command() returns non-zero status
+15.3k Golang : Delete certain files in a directory
+5.8k Golang : Find change in a combination of coins example
+10.4k Golang : Generate random integer or float number