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.
Syntax
func Create(name string) (*File, error)
Example: How to Use Go os.Create() function
package main
import (
"log"
"os"
)
func main() {
emptyFile, err := os.Create("emptyFile.txt")
if err != nil {
log.Fatal(err)
}
log.Println(emptyFile)
emptyFile.Close()
}
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
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.
What if the file already exists?