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
+12.7k Golang : Get current time
+16k Golang : Parse date string and convert to dd-mm-yyyy format
+6.7k Gogland : Where to put source code files in package directory for rookie
+27.1k Golang : Detect (OS) Operating System
+4.4k PHP : Hide PHP version information from curl
+12.9k Golang : How to determine if user agent is a mobile device example
+17.7k Golang : Fix cannot download, $GOPATH not set error
+11.2k Golang : How to display image file or expose CSS, JS files from localhost?
+17k Golang : Logging with logrus
+11.3k Golang : zlib compress file example
+4.7k PHP : Get client IP address