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
+7.7k Swift : Convert (cast) String to Float
+17.3k Golang : Clone with pointer and modify value
+36k Golang : Validate IP address
+7k Golang : Check if one string(rune) is permutation of another string(rune)
+33.3k Golang : How to check if slice or array is empty?
+9.7k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+8.9k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+5.8k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+17.8k Golang : Convert IPv4 address to decimal number(base 10) or integer
+7.5k Golang : How to execute code at certain day, hour and minute?
+13.9k Golang : Check if a file exist or not