Go os Package Create()

Introduction

To create a file, use the os.Create function.

It takes the name of the file, creates it in the current working directory.

It returns an os.File and possibly an error if it was unable to create it.

Here's an example program:

package main // w  w w .  jav  a 2s .c  om

import ( 
    "os" 
) 

func main() { 
    file, err  := os.Create("test.txt") 
    if err  != nil { 
        // handle the error here 
        return 
    } 
    defer file.Close() 

    file.WriteString("test") 
} 



PreviousNext

Related