Up to Main Index                              Up to Journal for June, 2012

                      JOURNAL FOR TUESDAY 5TH JUNE, 2012
______________________________________________________________________________

SUBJECT: Ideal opportunity - missed
   DATE: Tue Jun  5 23:44:32 BST 2012

Just had a long, 4 day weekend due to the Queen's Diamond Jubilee. What I
hoped would be a long 4 day WolfMUD hacking session didn't happen as I was
kept busy with other things instead. *sigh* never mind. At least while doing
mundane tasks I can think a lot about WolfMUD and "code in my head".

So I've got some documentation to finish off and then ... hrm ... do I release
the prototype then or write the unit tests first then release?

So far WolfMUD is 19 source files with about 1350 lines of code 550 lines of
comments - 1900 lines in total or 192K including blank lines and everything.
The source code for just the Java version of the server was over 2Mb alone.

That's one thing I like about Go - it's very compact yet still powerful and
expressive. For example this is a simple unique ID generator utility:


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ->8 - - - -


  // Copyright 2012 Andrew 'Diddymus' Rolfe. All rights reserved.
  //
  // Use of this source code is governed by the license in the LICENSE file
  // included with the source code.

  // Package uid provides a unique number generator. To get the next unique
  // number simply read from the Next channel:
  //
  //  MyId := <- uid.Next
  //
  package uid

  // UID is currently implemented as a uint64 giving IDs from 1 to
  // 18,446,744,073,709,551,615 or 0x1 to 0xFFFFFFFFFFFFFFFF or 18 Quintillion
  // IDs also known as 18 exaids. If this is not enough then the type for UID
  // can easily be changed. It also means you are probably trying to model
  // every atom of your world in WolfMUD or creating a very large galaxy!
  type UID uint64

  // Next is a read only channel used to retrieve the next ID number.
  var Next <-chan UID

  // init starts a goroutine to generate IDs on demand. The goroutine function
  // is a simple and efficient incrementing counter which blocks on a channel
  // and only generates the next ID when the current one is read.
  func init() {
    n := make(chan UID) // Create bi-directional channel
    Next = n            // Cast to exported read-only channel
    go func() {
      uid := UID(0)
      for {
        uid++
        n <- uid
      }
    }()
  }


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ->8 - - - -


It's thread safe as you just read the next ID from the channel and because the
channel blocks the Goroutine only wakes when you grab an ID and is idle the
rest of the time. In case your are counting that's 14 lines of code and 18
lines of comments. I could maybe make the code shorter as well. You can also
see why documenting everything takes a while - I'm trying to help other
developers who dive into the code as much as possible.

--
Diddymus


  Up to Main Index                              Up to Journal for June, 2012