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
+10.5k Golang : Forwarding a local port to a remote server example
+15.9k Golang : Find IP address from string
+14k Golang : Delete files by extension
+12.5k Golang : Reset buffer example
+18k nginx: [emerg] unknown directive "passenger_enabled"
+7.9k Golang : Populate slice with sequential integers example
+18k Golang : Append content to a file
+9.4k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+13.1k Golang : How to get Unix file descriptor for console and file
+5.3k Golang : Reverse by word
+40.8k Golang : Use wildcard patterns with filepath.Glob() example
+17.5k Golang : Close channel after ticker stopped example