> output, err := os.Create(o)
The short variable declaration you used to create `err` also shadows
`output`.

var err error
output, err = os.Create(o)


On Tue, Apr 12, 2022 at 8:53 AM Rory Campbell-Lange <r...@campbell-lange.net>
wrote:

> I have a command line programme that can output to either stdout or a
> named file. Output to stdout, as commented out in the small example below,
> works fine, but not to a named file.
>
> Attempting to write to a named file panics on go 1.17 on Linux with:
>
>     panic: runtime error: invalid memory address or nil pointer dereference
>     [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x46123d]
>
> I'm confused since both os.Stdout and os.Create are *os.File types, which
> implement the Write(b []byte) (n int, err error) method which fulfils the
> io.Writer interface. I pass around io.Writer types in my programme to
> facilitate testing.
>
> I've chosen io.WriteString because it will use StringWriter on the writer
> if the writer supports this method, which may perform better than a simple
> Write.
>
> Clearly I am missing something fundamental, or I have a stupid error in my
> programme. Help very much appreciated.
>
> Thanks
> Rory
>
>     package main
>
>     import (
>         "io"
>         "log"
>         "os"
>     )
>
>     func main() {
>
>         // o := "-"            // use stdin
>         o := "/tmp/output.txt" // or a file
>
>         var output io.Writer
>         if o == "-" {
>             output = os.Stdout
>         } else {
>             output, err := os.Create(o)
>             if err != nil {
>                 log.Fatalf("Could not create file %s, %s", output, err)
>             }
>             defer output.Close()
>         }
>
>         _, err := io.WriteString(output, "hello")
>         if err != nil {
>             log.Fatal(err)
>         }
>     }
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/YlUv/%2BH%2BHYguq4Nn%40campbell-lange.net
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAGabyPoZyBwx%2BdoVquRs9vsUJQjG46LJSk5MSLeyQpt95_0WZA%40mail.gmail.com.

Reply via email to