file i/o in a variety of languages

Walter Bright newshound2 at digitalmars.com
Fri Aug 27 11:25:07 PDT 2010


Trass3r wrote:
>> D doesn't look half bad:
> 
> Yeah that comment about Go says it all:
> 
>> It's rather amazing that given 30 years of evolution and language 
>> design, they've still managed to invent a new language that's as hard 
>> to write error-checking code in as C. Even Java's less verbose! – DK

Here's the Go example. I think Go has made a serious error in centering their 
design around error codes.
----------------------------------------
package main

import (
   "os"
   "bufio"
   "log"
)

func main() {
   file, err := os.Open("fileio.txt", os.O_RDWR | os.O_CREATE, 0666)
   if err != nil {
     log.Exit(err)
   }
   defer file.Close()

   _, err = file.Write([]byte("hello\n"))
   if err != nil {
     log.Exit(err)
   }

   _, err = file.Write([]byte("world\n"))
   if err != nil {
     log.Exit(err)
   }

   // seek to the beginning
   _, err = file.Seek(0,0)
   if err != nil {
     log.Exit(err)
   }

   bfile := bufio.NewReader(file)
   _, err = bfile.ReadBytes('\n')
   if err != nil {
     log.Exit(err)
   }

   line, err := bfile.ReadBytes('\n')
   if err != nil {
     log.Exit(err)
   }

   os.Stdout.Write(line)
}


More information about the Digitalmars-d mailing list