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
+9.1k Golang : How to use Gorilla webtoolkit context package properly
+8.9k Golang : Accept any number of function arguments with three dots(...)
+8.9k Android Studio : Image button and button example
+11.5k Golang : Change date format to yyyy-mm-dd
+5.2k Golang : Customize scanner.Scanner to treat dash as part of identifier
+15.6k Golang : Force download file example
+15.3k Golang : Accurate and reliable decimal calculations
+14.5k Golang : How to filter a map's elements for faster lookup
+7.5k Golang : Shuffle strings array
+13.4k Golang : Linear algebra and matrix calculation example
+16.2k Golang : How to check if input from os.Args is integer?
+19.9k Golang : How to get time from unix nano example