News for hackers. Stuff that matters.
Re:Gonix – Unix tools written in Go by quink on Friday May 08, 2015 @09:47PM (#9515105)
Re:Gonix – Unix tools written in Go by vvpan on Saturday May 09, 2015 @12:35AM (#9515507)
I don't know much about go, but taking a quick look at implementations - they seem to be written by a programming novice, and the are quite primitive. Don't mean to be negative, just my opinion.
Re:Gonix – Unix tools written in Go by polegone on Saturday May 09, 2015 @12:49AM (#9515536)
I am a novice, and that is one of the reasons why I started this project (to learn).
Re:Gonix – Unix tools written in Go by danieldk on Saturday May 09, 2015 @01:04AM (#9515580)

    bytes, _ := ioutil.ReadAll(os.Stdin)
    lines := strings.Split(string(bytes), "\n")
Tip: use bufio.Scanner.

    scanner := bufio.NewScanner(reader)
    scanner.Split(bufio.ScanLines)
    for scanner.Scan() {
      // Do stuff
    }
And you can iterate over the lines 'lazily'. If you don't want to consume \r's, make your own ScanLines :).
Re:Gonix – Unix tools written in Go by Someone on Saturday May 09, 2015 @03:14AM (#9515768)
Tail also doesn't always need to read its entire input. When given a file path, it can read backwards from the end of the file until it has found its n lines. That makes tail fast on huge files (as long as they have normal line lengths), but it also complicates the code.

And you may want to mmap the file, rather than open it. Whether that is a speeds up things depends on OS, OS version, file size, file system, available memory, phase of the moon, etc.

[deleted]
Re:Gonix – Unix tools written in Go by danieldk on Saturday May 09, 2015 @02:38AM (#9515729)
You mean bufio.ReadLine? As the documentation says, it's low-level, since you have to do the buffer allocation yourself. ReadBytes/ReadString are nicer interface-wise, but it allocates new buffers on every call.

I like Scanner because it provides a nice high-level interface, but still maintains an internal buffer, reducing GC pressure. Of course, it's not one size fits all.

Re:Gonix – Unix tools written in Go by polegone on Saturday May 09, 2015 @03:06AM (#9515760)
I tested it by on a several megabyte text file (not that large) and I can see a huge improvement in speed when I use a reader vs. loading the whole thing into memory as I did at first.

I can see now how much of a difference it can make on a really large file, like in the gigabyte range.

BTW, I deleted my earlier comment because the problem I had wasn't anything to do with bufio. I had just made an obvious mistake elsewhere in my code, which I've fixed now.

Re:Gonix – Unix tools written in Go by andrewchambers on Saturday May 09, 2015 @02:08AM (#9515693)
The problem is something like tail should not read the whole file into memory. tail works on a 100 gig file even with 100 megs of ram.
Re:Gonix – Unix tools written in Go by LaFolle on Saturday May 09, 2015 @01:08AM (#9515588)
Great going. This will also help other novice Go programmers learning the language, at the same time getting a sense of how to implement their own Unix commands.