Golang : Create Temporary File




Need to create a temporary file in Go? Easy. Below is the code in Go for creating a temporary file

 package main

 import (
 "fmt"
 "os"
 "io/ioutil"
 )

 func main () {

 file, err := ioutil.TempFile(os.TempDir(), "temp")

 if err != nil {
 panic(err)
 }

 fmt.Printf("Temp File created!")

 defer os.Remove(file.Name())

 }

Reference :

http://golang.org/pkg/io/ioutil/#TempFile

  See also : Golang : Create File





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