I'm trying to take a filepath string and create a Method on a type that stores it to parse the string, create the relevant directories if needed and create the file. I seem to be able to create the directory as I might expect but I get an error when trying to create the file. How do you create a file in a directory in Go? I'm very new to all of this and the NewFile fd property seems like it might be what I need but the uintptr is escaping me right now.
I'm pasting the method I'm having trouble with below. Thank you for any insight.
// Writes key value pairs to a tag file.
func (tf *TagFile) Create() {
// Create directory if needed.
basepath := path.Dir(tf.Filepath)
filename := path.Base(tf.Filepath)
if os.MkdirAll(basepath, 0666) != nil {
panic("Unable to create directory for tagfile!")
}
// Create the tagfile.
fileOut, err := os.Create(strings.Join([]string{basepath, filename}, "/"))
if err != nil {
panic("Unable to create tag file!")
}
defer fileOut.Close()
// Write fields and data to the file.
for key, data := range tf.Data {
_, err := io.WriteString(fileOut, fmt.Sprintln(formatField(key, data)))
if err != nil {
panic("Unable to write data to tagfile.")
}
}
}I'm trying to take a filepath string and create a Method on a type that stores it to parse the string, create the relevant directories if needed and create the file. I seem to be able to create the directory as I might expect but I get an error when trying to create the file. How do you create a file in a directory in Go? I'm very new to all of this and the NewFile fd property seems like it might be what I need but the uintptr is escaping me right now.I'm pasting the method I'm having trouble with below. Thank you for any insight.
// Writes key value pairs to a tag file.func (tf *TagFile) Create() {// Create directory if needed.basepath := path.Dir(tf.Filepath)filename := path.Base(tf.Filepath)if os.MkdirAll(basepath, 0666) != nil {
panic("Unable to create directory for tagfile!")}// Create the tagfile.fileOut, err := os.Create(strings.Join([]string{basepath, filename}, "/"))
if err != nil {panic("Unable to create tag file!")}defer fileOut.Close()// Write fields and data to the file.for key, data := range tf.Data {_, err := io.WriteString(fileOut, fmt.Sprintln(formatField(key, data)))
Thanks for your suggestions, they definitely helped simplify the code.I'm still getting an error though when I try to create the file. I created this snippet in hopes that it's more helpful. As I said I'm new to Go and I'm sure I'm overlooking something simple and thanks for any help.
Alright I think I have this fixed but I'm not sure I understand it. using 0777 directly for the FileMode does not result in the permission I would expect. using os.ModePerm does however and using that I can write the file just fine.
if os.MkdirAll(basepath, 0666) != nil {
panic("Unable to create directory for tagfile!")
}if err := os.MkdirAll(basepath, 0666); err != nil {
panic("Unable to create directory for tagfile! - " + err.Error())
}
As it includes the error's message (how MkdirAll failed) in addition to what you were trying to do at the time.