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.4k Golang : Flip coin example
+28.9k Golang : Generate random string
+13.4k Golang : Save(pipe) HTTP response into a file
+16.1k Golang : When to use init() function?
+27.8k Golang : Get first few and last few characters from string
+12.8k Golang : convert(cast) string to float value
+20.5k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+4.8k List of Golang XML tutorials
+3.9k Unix/Linux : How to pipe/save output of a command to file?
+9k Android Studio : Simple input textbox and intercept key example
+11k Android Studio : Highlight ImageButton when pressed on example