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
+13k Golang : Convert IPv4 address to packed 32-bit binary format
+17.3k Golang : Capture stdout of a child process and act according to the result
+5.7k PHP : Convert CSV to JSON with YQL example
+12.9k Golang : Sort and reverse sort a slice of bytes
+7k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+8.3k Golang : Multiplexer with net/http and map
+5.5k Golang : How to deal with configuration data?
+4.2k Detect if Google Analytics and Developer Media are loaded properly or not
+6.2k Fontello : How to load and use fonts?
+5k Golang : A program that contain another program and executes it during run-time
+6.2k nginx : force all pages to be SSL
+17.6k Golang : Linked list example