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
+11.6k Golang : How to detect a server/machine network interface capabilities?
+15.3k Golang : ROT47 (Caesar cipher by 47 characters) example
+7.6k Golang : Lock executable to a specific machine with unique hash of the machine
+10.5k Golang : Flip coin example
+26.2k Golang : Get executable name behind process ID example
+10.8k Golang : Sieve of Eratosthenes algorithm
+8.1k Golang : Find relative luminance or color brightness
+7.2k Golang : Example of custom handler for Gorilla's Path usage.
+9.3k Golang : Scramble and unscramble text message by randomly replacing words
+10.7k Golang : Get UDP client IP address and differentiate clients by port number
+36k Golang : Save image to PNG, JPEG or GIF format.
+13k Golang : Handle or parse date string with Z suffix(RFC3339) example