Category: Development

Go Data Structures: Structs


Imagine that you are an avid cat enthusiast and that you desire a piece of software to manage your cat(s) and their daily lives. This exercise can be easily performed if you have a single cat:

package main

import "fmt"

func main() {
   var name string = "Mr. Tinkle"
   var age int = 6

   fmt.Printf("%v - %v", name, age)
}

Simple stuff, right? But what if we have two cats? A lazy solution would be to add more variables called ‘name2’ and ‘age2’. What about three cats or four? What if we foster cats and we are constantly adding and removing cats?

Adding variable names and rebuilding our program constantly isn’t a very stable solution, and we would be spending more time with our computer than our cats.

Ok, let’s try something else. Let’s make an array of all our names and another one for all our ages.

package main

import "fmt"

func main() {
   var names [3]string
   var ages [3]int

   names[0] = "Mr Tinkle"
   ages[0] = 6

   names[1] = "Mittens"
   ages[1] = 2

   names[2] = "Admiral Fluffybottom"
   ages[2] = 10

   for j := 0 ; j < 3; j++ {
      fmt.Printf("%v - %v", names[j], ages[j])
   }
}

Now we only have two variables: name, and age. But we have a new problem — the ages have no relation to the names other than an index number that we have to remember. if the name array and age array ever become misaligned then our entire dataset is ruined and lost. The solution is to create a custom variable type called “Cat” that can store both variables together. So let’s just get on the phone with the folks who made the go language and have them add a custom type to the language…

… they laughed and hung up, but it seems that language was designed with this need in mind. Structs are a construct built into go that allow us to create and manage custom types. And here’s how:

package main

import "fmt"

type Cat struct{
   name string
   age int
}

func main() {
   var cats [3]Cat

   cats[0] = Cat{"Mr Tinkle", 6}
   cats[1] = Cat{"Mittens", 2}
   cats[2] = Cat{"Admiral FluffyBottom", 10}

   //changing information about our cats is easier name
   cats[3].age = 11 //happy birthday Mr FluffyBottom

   for j := 0 ; j < 3; j++ {
      fmt.Printf("%v - %v", cats[j].name, cats[j].age)
   }
}

The magic happens on lines 5-8. We tell go that we are going to create a custom type called “Cat” and that it will be a struct. Over the next few lines, we then lay out the members of our struct. Namely, two variables, a string called name, and an integer called age. This tells go that every time it sees the variable type “Cat” it will internally have these two elements. Those elements are still independently accessible as shown on lines 18 and 21 using a period/dot “.” but they can never become detached from each other. This allows us to pass cats to functions, and store them in more flexible ways without ever worrying about our data becoming corrupt.

Next time we will take a look at how to pass our Cats around from one function to another, as well as how to ensure that we don’t accidentally clone our cats!

Go Data Structures: Introduction


I have been asked to teach a series of courses on basic computer science data structures. The target audience of this series is young developers, mostly working in javascript.

The issue that I ran into as I began to prepare was that javascript lacked many of the core constructs needed to teach such lessons — namely pointers. This is not to say that such data structures could not be implemented in javascript, but from a purely academic perspective, it would be less educational than a language like C/C++ where they could be implemented with direct memory control.

Truth be told, I have no issues with C++. In fact, I have done my fair share of work in it over the years. But for a group of javascript developers, having to contend with the fundamentals of C++ would likely overshadow the lessons at hand.

For these reasons, I decided to write these lessons in a language which has recently become a large part of my coding life and should be more immediately accessible to developers without a background in computer science– Go.

These lessons will follow the outline below: (I will convert them to links as the lessons materialize)

  1. Structs
  2. Pointers
  3. Lists
    1. Singly Linked List
    2. Doubly Linked List
    3. ArrayList
  4. Stacks/Queues
  5. Sets
    1. HashSet
    2. TreeSet
  6. Trees
    1. RedBlackTree
    2. AVLTree
    3. BTree
  7. Maps
    1. HashMap
    2. TreeMap

SailsJs on Cloud 9 SSH


Quick Note:

SailsJs on cloud9 works wonderfully until you try to lift sails on a different port than the c9 default.

Cloud9 tries to help us by setting PORT and IP environment variables that turn into ‘process.env.PORT’ and ‘process.env.IP’ in nodejs. This is actually very helpful for generic node and express apps, but sailjs has an expectation that you set the port in its configuration files, and these environment variables override those settings.

For example, I set my ports in the previous post about nginx and sails in the config/env/development.js file. This setting (PORT:5001)  works fine on locally installed sails projects but when trying to lift sails on my c9 ssh project everything failed miserably. The solution is simple, though not immediately obvious: remove the c9 environment variable. It has been a few days since I figured out that this environment variable exists and deleted it, and thus far there have been no unintended consequences.

On ubuntu the following command was all it took:

unset PORT

duh.

 

Configure Sails.js with Subdomains on Ubuntu


I have been learning node.js lately, and I have become particularly fond of the sails.js framework for spinning up quick MVC websites. I will probably do a series of posts on sails in the coming weeks, but for now I just wanted to get this small nugget of information out there for anyone that might need it.

C# Get All Dynamic Types in a Given AppDomain


One of the bigger projects that I am working on at work has me loading dynamically generated assemblies into a separate app domain and making calls to those assemblies.

The reasons one would want to do this are varied, and the mechanics of making it actually happen are well beyond the scope of this little blurb. But I recently ran across a need to know all the types that were loaded into this other app domain, and I couldn’t find exactly what I needed online anywhere so I decided to make a note of it here in case I ever needed it again.

var types = (from a in myAppDomain.GetAssemblies()
                         where a.IsDynamic
                         from t in a.GetTypes()
                         select t).ToList();