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.1k Golang : Pagination with go-paginator configuration example
+20.9k Golang : Join arrays or slices example
+6.3k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+12.3k Golang : Linear algebra and matrix calculation example
+8.9k Golang : Load ASN1 encoded DSA public key PEM file example
+14.3k Golang : How to get Unix file descriptor for console and file
+41.6k Golang : Get hardware information such as disk, memory and CPU usage
+14.5k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+10.7k Golang : Change date format to yyyy-mm-dd
+4.7k How to check with curl if my website or the asset is gzipped ?
+10.8k Golang : Convert(cast) float to int