Here, We will see how to create file in go. We can do it by using Create() function in os package in go golang.
Function prototype:
func Create(name string) (*File, error)
Input Parameter:
name : file name
Create() function creates the named file. If the file already exists, it is truncated. By default, It creates file with mode 0666 (before umask).
Return value:
Create() function in os package returns
the file pointer.
If there is an error, it will be of type
*PathError.
Example with code:
package main
import (
"fmt"
"os"
"log"
)
func main() {
file_name := "/Users/hello.go"
file, err := os.Create(file_name)
if err != nil {
log.Fatal(err)
}
fmt.Println("File Pointer: ", file)
file.Close()
}
Output:
$ sudo go run sample.go
File Pointer: &{0xc000050180}
$ ls -lrt hello.go
-rw-r--r-- 1 john staff 0 May 5 19:48 hello.go
To learn more about golang, Please refer given below link:
https://www.techieindoor.com/go-lang-tutorial/
References:
https://golang.org/doc/
https://golang.org/pkg/