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
+6k Golang : Create new color from command line parameters
+26.3k Golang : Convert(cast) string to uint8 type and back to string
+8.2k Golang : Configure Apache and NGINX to access your Go service example
+6.1k Linux/Unix : Commands that you need to be careful about
+7.3k Golang : How to convert strange string to JSON with json.MarshalIndent
+7.9k Golang : What fmt.Println() can do and println() cannot do
+35.8k Golang : Integer is between a range
+4.9k Google : Block or disable caching of your website content
+17.6k Golang : [json: cannot unmarshal object into Go value of type]
+12.5k Golang : Arithmetic operation with numerical slices or arrays example
+9.4k Golang : Convert(cast) string to int64
+18.3k Golang : Read binary file into memory