OK. Got it. Full code and return values are:
https://gist.github.com/brk0v/0a765b3a107128c0e7a208f2a9402db7 I expect that my alignment write from an array will work as it works with a slice. Thank you. On Fri, 29 Dec 2017 at 01:36, Ian Lance Taylor <i...@golang.org> wrote: > On Thu, Dec 28, 2017 at 2:31 PM, Viacheslav Biriukov > <v.v.biriu...@gmail.com> wrote: > > > > Could you please help me with the following code with O_DIRECT and > array. I > > tried to implement direct io and stuck with weird behaviour of alignment > > writes created from array. > > > > > > Following code doesn't work: > > > > > > package main > > > > > > import ( > > > > "os" > > > > "syscall" > > > > ) > > > > > > func main() { > > > > var p [2 << 16]byte > > > > for i := 0; i < len(p); i++ { > > > > p[i] = byte(' ' + i%('~'-' ')) > > > > } > > > > > > f, _ := os.OpenFile("/tmp/1.txt", > > os.O_TRUNC|os.O_CREATE|os.O_WRONLY|syscall.O_DIRECT, 0666) > > > > defer f.Close() > > > > > > f.Write(p[:]) > > > > } > > > > > > But, if I make a copy of that array – it's OK: > > > > > > package main > > > > > > import ( > > > > "os" > > > > "syscall" > > > > ) > > > > > > func main() { > > > > var p [2 << 16]byte > > > > for i := 0; i < len(p); i++ { > > > > p[i] = byte(' ' + i%('~'-' ')) > > > > } > > > > > > f, _ := os.OpenFile("/tmp/1.txt", > > os.O_TRUNC|os.O_CREATE|os.O_WRONLY|syscall.O_DIRECT, 0666) > > > > defer f.Close() > > > > > > var a []byte // <----- copy > > > > a = append(a, p[:]...) // <----/ > > > > f.Write(a) > > > > } > > > > > > Can't figure this out. And how to catch such behaviour in runtime. > > 1) Always check your errors. You are ignoring the error return of > os.OpenFile and f.Write and f.Close. > > 2) You asked for help but all you said was that your code "doesn't > work." Tell us what you expected, and precisely what happened > instead. > > Ian > -- Sent from Gmail Mobile -- 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. For more options, visit https://groups.google.com/d/optout.