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.5k Golang : Copy map(hash table) example
+6.6k Golang : Reverse by word
+10.3k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+16.4k Golang : File path independent of Operating System
+7.2k Golang : How to convert strange string to JSON with json.MarshalIndent
+10k Golang : Use regular expression to get all upper case or lower case characters example
+17.5k Golang : Upload/Receive file progress indicator
+37.9k Golang : Read a text file and replace certain words
+9.3k Facebook : Getting the friends list with PHP return JSON format
+12.1k Golang : calculate elapsed run time
+7.9k Findstr command the Grep equivalent for Windows