このコードは nil 逆参照になります。
tmpfile, _ := ioutil.TempFile("", "testing")
defer tmpfile.Close()
tmpwriter := bufio.NewWriter(tmpfile)
defer tmpwriter.Flush()
tmpwriter.WriteString("Hello World\n")
a := make([]*bufio.Writer, 1)
a = append(a, tmpwriter)
a[0].WriteString("It's Me") //Error here
このコードでは、nil の逆参照は発生しませんが、実際には一時ファイルには何も書き込まれません。
tmpfile, _ := ioutil.TempFile("", "testing")
defer tmpfile.Close()
tmpwriter := bufio.NewWriter(tmpfile)
defer tmpwriter.Flush()
tmpwriter.WriteString("Hello World\n")
a := make([]bufio.Writer, 1)
a = append(a, *tmpwriter) //Dereferencing seems to cause the first string not to get written
a[0].WriteString("It's Me")
ここで欠けている原則は何ですか?ライターのスライスを保存する慣用的な方法は何ですか?最初のケースで nil を引き起こす内部で何が起こっているのか、2番目のケースではライター自体に副作用を引き起こすポインター逆参照のように見えるものは何ですか?