github.com/enetx/g@v1.0.80/examples/files/tmp_file.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/enetx/g"
     7  )
     8  
     9  func main() {
    10  	// Create a temporary file in the current directory with a ".txt" extension
    11  	f := g.NewFile("").CreateTemp("./", "*.txt").Unwrap().Write("some text").Unwrap()
    12  
    13  	// Alternatively, create a temporary file without specifying the extension
    14  	// f := g.NewFile("").CreateTemp().Unwrap().Write("some text").Unwrap()
    15  
    16  	// Print the path and content of the temporary file
    17  	fmt.Println(f.Path().Unwrap(), f.Read().Unwrap())
    18  
    19  	// Calculate the MD5 hash of the file's content and print it
    20  	f.Read().Unwrap().Hash().MD5().Print()
    21  
    22  	// Remove the temporary file and print whether it still exists
    23  	fmt.Println(f.Remove().Unwrap().Exist())
    24  }