10 examples of 'golang temp file' in Go

Every line of 'golang temp file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
46func TestTempFile(t testing.TB, prefix string) (File, func()) {
47 f, err := ioutil.TempFile("", prefix)
48 if err != nil {
49 t.Fatal(err)
50 }
51
52 cleanup := func() {
53 _ = f.Close()
54 err = Remove(f.Name())
55 if err != nil {
56 t.Fatal(err)
57 }
58 }
59
60 return f, cleanup
61}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

14func CreateTempfile(b []byte, t *testing.T) *os.File {
15 f, err := ioutil.TempFile(os.TempDir(), "")
16 if err != nil {
17 t.Fatal(err)
18 }
19
20 if len(b) > 0 {
21 _, err = f.Write(b)
22 if err != nil {
23 t.Fatal(err)
24 }
25 }
26
27 return f
28}
120func (u UnitSystem) Temp(tempC float32) (res float32, unit string) {
121 if u == UnitsMetric || u == UnitsMetricMs {
122 return tempC, "°C"
123 } else if u == UnitsImperial {
124 return tempC*1.8 + 32, "°F"
125 } else if u == UnitsSi {
126 return tempC + 273.16, "°K"
127 }
128 log.Fatalln("Unknown unit system:", u)
129 return
130}
240func cleanFile(fileURLStr string) {
241 u, err := url.Parse(fileURLStr)
242
243 if err != nil {
244 fmt.Println("error parsing the file URL with SAS", err)
245 os.Exit(1)
246 }
247
248 p := createFilePipeline(*u)
249 fileURL := azfile.NewFileURL(*u, p)
250
251 _, err = fileURL.Delete(context.Background())
252 if err != nil {
253 fmt.Println("error deleting the file ", err)
254 os.Exit(1)
255 }
256}
114func (c *cache) mkTemp() string {
115 c.Lock()
116 next := c.next
117 c.next++
118 c.Unlock()
119 return filepath.Join(c.dir, fmt.Sprintf("tmp/%d", next))
120}
311func createTempFile(data []byte) (fileName string, err error) {
312 // Avoid overwriting a file in the currect directory by choosing an unused
313 // file name.
314 baseName := "temp"
315 tempFileName := baseName
316 tryIndex := 0
317 for {
318 if _, err := os.Stat(tempFileName); err == nil {
319 tempFileName = baseName + strconv.Itoa(tryIndex)
320 tryIndex++
321 } else {
322 break
323 }
324 }
325
326 readWritePermissions := os.FileMode(0664)
327 err = ioutil.WriteFile(tempFileName, data, readWritePermissions)
328 if err != nil {
329 return "", err
330 }
331
332 return tempFileName, nil
333}
9func main() {
10 // Create a new file
11 f, err := os.Create("file.txt")
12
13 // Check if there were any errors and if so, log them
14 if err != nil {
15 log.Fatalln(err)
16 }
17
18 // Close and then remove the file after main finishes execution
19 defer func() {
20 if err := f.Close(); err != nil {
21 log.Fatalln(err)
22 }
23 if err := os.Remove("file.txt"); err != nil {
24 log.Fatalln(err)
25 }
26 }()
27
28 // Get and print the files name
29 //
30 // Name returns the name of the file as presented to Open.
31 fmt.Println(f.Name())
32}
193func WriteTempFile(prefix string, data []byte, mode os.FileMode) (string, error) {
194 path := TempPath("", prefix)
195 if err := ioutil.WriteFile(path, data, mode); err != nil {
196 return "", err
197 }
198 return path, nil
199}
90func GenerateDummyFile(filename string, size int) (*os.File, error) {
91 tmpFile, err := ioutil.TempFile(os.TempDir(), "go-stream-bench-"+filename)
92 if err != nil {
93 return nil, err
94 }
95
96 b := make([]byte, size)
97 _, err = rand.Read(b)
98 if err != nil {
99 return nil, err
100 }
101
102 _, err = tmpFile.Write(b)
103 if err != nil {
104 return nil, err
105 }
106
107 tmpFile.Seek(0, io.SeekStart)
108
109 return tmpFile, nil
110}
81func (tc *TestConfig) createTempFile(filename string, content []string) error {
82 tmpFile, err := ioutil.TempFile("", filename)
83 if err != nil {
84 tc.T.Fatal(err)
85 }
86 tc.Filepath = tmpFile.Name()
87 for _, s := range content {
88 if _, err := tmpFile.WriteString(s + "\n"); err != nil {
89 return err
90 }
91 }
92 if err := tmpFile.Close(); err != nil {
93 return err
94 }
95 return nil
96}

Related snippets