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
+11k Golang : 2 dimensional array example
+16.1k Golang : Find smallest number in array
+32.8k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+8.9k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+14.7k Golang : Read a file line by line
+15.2k Golang : Send email and SMTP configuration example
+3.5k Java : Generate multiplication table example
+9.6k Golang : Replace a parameter's value inside a configuration file example
+18.5k Golang : Append content to a file
+5.7k Golang : Output or print out JSON stream/encoded data
+9.1k Golang : Read file and convert content to string
+13.9k Golang : Find location by IP address and display with Google Map