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.3k Golang : Remove or trim extra comma from CSV
+8.9k Golang : Translate language with language package example
+12.8k Android Studio : Use image as AlertDialog title with custom layout example
+9.8k Golang : Removes punctuation or defined delimiter from the user's input
+34.2k Golang : Validate IP address
+6.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+5.6k Golang : How to setup a disk space used monitoring service with Telegram bot
+12.3k Golang : Verify token from Google Authenticator App
+7.6k Golang : Executing and evaluating nested loop in html template
+5.8k CloudFlare : Another way to get visitor's real IP address
+28.3k Golang : Get and Set User-Agent examples
+5k Golang : Function as an argument type example