Golang os.Create() Method

Golang os.Create() function is used to create a file.

The Create() function creates a new file if it does not exist, with the file mode 0666 for reading and writing.

The Create() function truncates the file if it exists, meaning its contents are removed without deleting it.

The returned file descriptor is open for reading and writing.

If there is some problem, the function returns an error of type *os.PathError.


func Create(name string) (*File, error)

Example: How to Use Go os.Create() function

package main

import (

func main() {
  emptyFile, err := os.Create("emptyFile.txt")
  if err != nil {

That’s it.

2 thoughts on “Golang os.Create() Method”

  1. Hi, thank you for the snipped.
    Can you create the emptyFile.txt file in a diferent directory as hello.go?

    Thank you for your help.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.