FAQ
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.")

         }

     }

}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Minux at May 29, 2013 at 6:58 pm

    On Thu, May 30, 2013 at 2:49 AM, Scott Turnbull wrote:

    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 {

    you should change 0666 to 0777. or you won't be able to create
    files in a dir with perm 0666.
    panic("Unable to create directory for tagfile!")

    }


    // Create the tagfile.

    fileOut, err := os.Create(strings.Join([]string{basepath, filename}, "/"))

    you'd better use path/filepath.Join for this.
    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)))

    why not use fmt.Fprintln directly?
    if err != nil {

    panic("Unable to write data to tagfile.")

    }

    }

    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Scott Turnbull at May 29, 2013 at 8:03 pm
    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.

    http://play.golang.org/p/5T51hBH3Ha
    On Wednesday, May 29, 2013 2:58:04 PM UTC-4, minux wrote:


    On Thu, May 30, 2013 at 2:49 AM, Scott Turnbull <stream...@gmail.com<javascript:>
    wrote:
    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 {

    you should change 0666 to 0777. or you won't be able to create
    files in a dir with perm 0666.
    panic("Unable to create directory for tagfile!")

    }


    // Create the tagfile.

    fileOut, err := os.Create(strings.Join([]string{basepath, filename}, "/"))

    you'd better use path/filepath.Join for this.
    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)))

    why not use fmt.Fprintln directly?
    if err != nil {

    panic("Unable to write data to tagfile.")

    }

    }

    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Andrey mirtchovski at May 29, 2013 at 8:05 pm
    it would help if you share what the error was. your code works for me as is.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at May 29, 2013 at 8:16 pm

    On Thu, May 30, 2013 at 4:03 AM, Scott Turnbull wrote:

    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.

    http://play.golang.org/p/5T51hBH3Ha
    Can't spot anything obviously wrong. Please provide more details on
    the error (for example, what does your program output?)

    it makes sense to manually try to do the work the program tries to do
    and see if it could succeed.
    for example, if your program successfully makes a directory called /foo/bar,
    and then fails to create /foo/bar/somefile, you should try to create that
    file
    in the path manually to see if it works.

    ps: i suggest you include some contextual information in panics (or better,
    return an error in Create() method), so that when error occurs, you know
    exactly what's happening.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Scott Turnbull at May 29, 2013 at 8:14 pm
    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.
    On Wednesday, May 29, 2013 2:58:04 PM UTC-4, minux wrote:


    On Thu, May 30, 2013 at 2:49 AM, Scott Turnbull <stream...@gmail.com<javascript:>
    wrote:
    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 {

    you should change 0666 to 0777. or you won't be able to create
    files in a dir with perm 0666.
    panic("Unable to create directory for tagfile!")

    }


    // Create the tagfile.

    fileOut, err := os.Create(strings.Join([]string{basepath, filename}, "/"))

    you'd better use path/filepath.Join for this.
    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)))

    why not use fmt.Fprintln directly?
    if err != nil {

    panic("Unable to write data to tagfile.")

    }

    }

    }
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at May 29, 2013 at 8:19 pm

    On Thu, May 30, 2013 at 4:14 AM, Scott Turnbull wrote:

    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.
    the actual permission is still subject to your umask setting.

    what do you mean by "using os.ModePerm does"?
    could you please show what changes made the program working correctly?

    using 0777 for os.MkdirAll normally should work just ok (except when you're
    concerned about others reading your directories, but at least you need rwx
    perm. for the owner if you want to use the directory normally).

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Scott Turnbull at May 29, 2013 at 9:06 pm
    I am obviously tangled up somewhere. I tried setting the code back to what
    I believe was giving me the error but it no longer does and works as I
    would expect. There must have been some other error I overlooked and
    fixed. Thanks for your feedback and for my confusion.

    On Wed, May 29, 2013 at 4:18 PM, minux wrote:

    On Thu, May 30, 2013 at 4:14 AM, Scott Turnbull wrote:

    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.
    the actual permission is still subject to your umask setting.

    what do you mean by "using os.ModePerm does"?
    could you please show what changes made the program working correctly?

    using 0777 for os.MkdirAll normally should work just ok (except when you're
    concerned about others reading your directories, but at least you need rwx
    perm. for the owner if you want to use the directory normally).
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Carlos Castillo at May 30, 2013 at 12:11 pm
    One suggestion, even if you decide to keep your panic-oriented error
    handling strategy, you should still include the error in the panic, so you
    can see what the error claims is wrong, instead of just what you assume is
    wrong.

    For example, your os.MkdirAll section:

    if os.MkdirAll(basepath, 0666) != nil {

             panic("Unable to create directory for tagfile!")

    }


    Would be more useful (to debug) as:

    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.
    On Wednesday, May 29, 2013 2:06:40 PM UTC-7, Scott Turnbull wrote:

    I am obviously tangled up somewhere. I tried setting the code back to what
    I believe was giving me the error but it no longer does and works as I
    would expect. There must have been some other error I overlooked and
    fixed. Thanks for your feedback and for my confusion.


    On Wed, May 29, 2013 at 4:18 PM, minux <minu...@gmail.com <javascript:>>wrote:
    On Thu, May 30, 2013 at 4:14 AM, Scott Turnbull <stream...@gmail.com<javascript:>
    wrote:
    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.
    the actual permission is still subject to your umask setting.

    what do you mean by "using os.ModePerm does"?
    could you please show what changes made the program working correctly?

    using 0777 for os.MkdirAll normally should work just ok (except when
    you're
    concerned about others reading your directories, but at least you need rwx
    perm. for the owner if you want to use the directory normally).
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 29, '13 at 6:49p
activeMay 30, '13 at 12:11p
posts9
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase